计算PHP中两个日期之间的差异 [英] Calculates difference between two dates in PHP

查看:82
本文介绍了计算PHP中两个日期之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MySql数据库服务器中有几篇文章,每篇文章中的信息内容之一是日期和时间,格式为 datetime (例如2010-11-26 21 :55:09)发帖的时间.

i have a couple of posts in my MySql database server, one of the info content in each post is the date and time in the format datetime (Ex. 2010-11-26 21:55:09) when the post was made.

因此,我想使用函数NOW()从SQL Server中检索实际日期和时间,并计算多少秒,几分钟,几小时或几天前发布信息.

So, i want to retrive the actual date and time from the SQL server with the function NOW() and calculates how many seconds or minutes or hours or days ago was post the info.

我不知道如何创建此php脚本,但我知道肯定已经做好了,所以谢谢您的帮助.

I dont know how to create this php script but i know that for sure is allready made, so thanks for any help.

推荐答案

正如billythekid所说,如果您使用的是PHP5.3 +,则可以使用date_diff()函数;如果您使用的不是PHP5.3 +,则可以使用多种方法.如其他海报所示.如果想知道将时间划分为"hours:mins:secs"层次结构,MySQL中最快的方法是使用

As billythekid said, you can use the date_diff() function if you are using PHP5.3+, if you are not then there are various methods. As shown by other posters. The quickest method in MySQL if you want to know the time split in to the "hours:mins:secs" hierarchy is to use the TIMEDIFF() function.

SELECT TIMEDIFF(NOW(), '2010-11-26 12:00:00');

如果要以秒为单位,请使用MySQL或PHP中的unix时间戳功能,可以使用strtotime()快速将MySQL日期转换为PHP.

If you want it as seconds, use the unix timestamp features in MySQL or in PHP, you can convert MySQL dates to PHP quickly using strtotime().

这篇关于计算PHP中两个日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆