我试图在几分钟内得出2个不同日期之间的差异,但输出不正确。
例如:
$then = "2017-01-23 18:21:24";
//Convert it into a timestamp.
$then = strtotime($then);
//Get the current timestamp.
$now = time();
//Calculate the difference.
$difference = $now - $then;
//Convert seconds into minutes.
$minutes = floor($difference / 60);
echo $minutes;
输出611分,从“2017-01-23 18:21:24”到“2017-01-24 12:36:24”超过611分钟是错误的。我的代码不正确吗?
答案 0 :(得分:2)
尝试设置默认时区
date_default_timezone_set('Europe/Copenhagen');
Ofc将欧洲/哥本哈根改为适合您需求的那个。
答案 1 :(得分:0)
如果您正在使用或能够使用PHP 5.3.x或更高版本,则可以使用其DateTime对象功能:
$date_a = new DateTime('2010-10-20 08:10:00');
$date_b = new DateTime('2008-12-13 10:42:00');
$interval = date_diff($date_a,$date_b);
echo $interval->format('%h:%i:%s');
您可以通过各种方式使用该格式,并且在DateTime对象中有日期后,您可以利用许多不同的功能,例如通过普通运算符进行比较。有关详细信息,请参阅手册:http://us3.php.net/manual/en/datetime.diff.php
答案 2 :(得分:0)
我已经检查了你的代码是否完美有效如果有任何疑问see your result
但你错了,所以要忽略这个设定你的时区。