[英]How to calulate the difference between a MySQL timestamp and the current time in PHP
I'm trying to calculate the difference between a timestamp retrieved from a MySQL database and the current time. 我正在尝试计算从MySQL数据库检索的时间戳与当前时间之间的差异。
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. 正如@RemoteSojourner所提到的,我得到了UNIX时间戳格式的当前时间(以秒为单位返回时间),我从数据库中获取时间戳(使用ORM)并将其转换为UNIX时间戳,然后减去两个时间戳。
$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) select timediff(now(),now() - interval 1 hour)
您可以使用strtotime函数将MySQL时间戳解析为Unix时间戳,可以在PHP日期函数中进一步解析或格式化。
Retrieve the datetime from mysql like this 像这样从mysql中检索日期时间
SELECT UNIX_TIMESTAMP(`time_col`) FROM `tablename`...
AND compare it to time()
并将其与time()
进行比较
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.