Reputation:
Have timestamp for the task in the MySQL - 1654423473. https://www.epochconverter.com/ correctly transforming this timestamp into the GMT: Sunday, 5 June 2022 г., 10:04:33. That is date of task creation. Using this Yii2 method I outputting this data in the view -
public function getWasOnSite()
{
$timePeriod = strtotime('now') - strtotime($this->dt_add);
return \Yii::$app->formatter->asDuration($timePeriod);
}
And expecting some days, minutes, etc, but seeing this -1450 years, 11 months, 30 days, 17 hours, 3 minutes, 45 seconds ago
How to remake into the correct variant?
Upvotes: 0
Views: 38
Reputation: 6144
If you already have unix timestamp in $this->dt_add
then you shouldn't pass it into strtotime()
. If you pass unix timestamp to that function it will return false
.
Then if you do strtotime('now') - false
the false
is converted to 0 and you are left with current timestamp in $timePeriod
instead of difference.
You should directly subtract $this->dt_add
from current timestamp.
Also, you don't need to call strtotime('now')
to get current timestamp. You can use time()
function instead.
So, your whole code can look like this:
public function getWasOnSite()
{
$timePeriod = time() - $this->dt_add;
return \Yii::$app->formatter->asDuration($timePeriod);
}
Upvotes: 1