<?php
$currtime = date(‘m/d/Y H:m:s’, time());
$currunixtime = time();
$currtime2unix = strtotime($currtime);
echo "Currtime $currtime ";
echo "Currunixtime $currunixtime ";
echo "Currtime2unix $currtime2unix ";
?>
I was expecting $currunixtime and $currtime2unix to be the same, but I can’t figure out why they aren’t
Alex
It’s the time format that’s the problem.
It’s not H:m:s
It should be H:i:s
See: http://php.net/manual/en/function.date.php for the formatting parameters.
Paul is right, the discrepancy is significant not just a second or two. Any ideas how to
correct this.
I need to calculate the difference between current time and the time saved in my database’s datetime column.
I figured I woud convert the db column to integer with strtotime(…) and subtract it from time().
So what’s the best/accurate way of doing this.
It’s not time() that’s responsible.
You’ll see the number of seconds discrepancy with the following:
echo $currunixtime - $currtime2unix;
For me now it’s 1200 seconds, and a short time ago it was around 400
The discrepancy remains constant until the seconds tick over to a new minute, at which point the discrepancy climbs by another 60.
because you’re calling time() twice; they may be executing at 2 different seconds in time. (I’m assuming by discrepancy you mean 1-2 second, and not like… a day or year or something)
try
<?php
$currunixtime = time();
$currtime = date('m/d/Y H:m:s', $currunixtime);
$currtime2unix = strtotime($currtime);
echo "Currtime $currtime ";
echo "Currunixtime $currunixtime ";
echo "Currtime2unix $currtime2unix ";
?>
It’s the conversion to a displayed date that’s causing the issue.
You see the time difference with
echo strtotime(date(‘m/d/Y H:m:s’, time())) - time();
Yep, the format was the culprit. Thank you Paul.