How to calulate the difference between a MySQL timestamp and the current time in PHP
I'm trying to calculate the difference between开发者_JS百科 a timestamp retrieved from a MySQL database and the current time.
Appreciate the help.
As mentioned by @RemoteSojourner, I got the current time in a UNIX timestamp format (which returns time in seconds), I got the timestamp from the DB (using an ORM) and converted that to a UNIX timstamp too and then subtracted the two timestamps.
$current_time = strtotime("now");
$last_access_time = strtotime($this->last_access);
$inactivity_duration = $current_time - $last_access_time;
This example makes the difference between now and one hour ago.
select timediff(now(), now() - interval 1 hour)
You can use the strtotime function to parse the MySQL timestamp into a Unix timestamp can be further parsed or formatted in the PHP date function.
Retrieve the datetime from mysql like this
SELECT UNIX_TIMESTAMP(`time_col`) FROM `tablename`...
AND compare it to time()
精彩评论