Reputation: 14002
so i have this code in php :
$now= new \Datetime("UTC");
$lv=$user->getLastVisit();
$interval =$lv->diff($now,true);
print_r($interval);
print_r("<br>".$lv->format("Y-m-d H:i:s"));
print_r("<br>".$now->format("Y-m-d H:i:s"));
exit(0);
and this is the output :
DateInterval Object ( [y] => 0 [m] => 0 [d] => 0 [h] => 6 [i] => 59 [s] => 6 [invert] => 0 [days] => 0 )
2013-04-09 23:44:21
2013-04-09 23:45:15
so the difference result is 6 hours 59 minutes and 6 seconds ! but if i do the difference manually i have 54 seconds !! so what's wrong with this DateTime::diff function ?
Edit:
this is the var_dump($user->getLastVisit());
2013-04-09 23:54:59object(DateTime)#320 (3) { ["date"]=> string(19) "2013-04-09 23:44:21" ["timezone_type"]=> int(3) ["timezone"]=> string(19) "America/Los_Angeles" }
Upvotes: 0
Views: 204
Reputation: 28901
The last login time should be 23:44 UTC, not Los_Angeles. (As of this writing, it still isn't 2013-04-09 23:44:21 in Los Angeles!)
It seems that you might be storing the last login time in your database as UTC, but when pulling back out of your database PHP is using the local/default timezone, and treating it as America/Los_Angeles
.
See here: http://3v4l.org/elbLY
Notice that I'm setting $now
to the exact same time (in UTC) that you had, and I'm using your $lv
time as well, first in Los_Angeles timezone and then in UTC.
The first interval is what you got, the second interval is showing the 54 seconds correctly.
So you need to fix your $user->getLastVisit()
method and ensure that it hands you that date in the UTC, and your diff will work.
Upvotes: 1