【发布时间】:2012-06-14 23:46:06
【问题描述】:
时间令人困惑......如何在php中计算从PM到AM所经过的时间?
例如 22:00:00 (10pm) 到 02:00:00 (02am) 应该给 04 hours elapsed。相反,我的代码返回 -08 hours
function hours_elapsed()
{
$timestart = strtotime("22:00:00");
$timestop = strtotime("02:00:00");
$time_diff = $timestop - $timestart; //time difference
return gmdate("h", $total_hours);
}
很明显,代码是以 24 小时格式计算的,所以它当然会返回 -08,但是如果没有 24 小时限制,怎么可能在经过午夜标记时获得时间?
【问题讨论】:
-
您的代码不知道 $timestop 是第二天,因此它认为经过的时间是负数。
-
时代的源泉是什么?它们是否以 xx:xx:xx 字符串开头?
-
@codewaggle 我从字段为
TIME的列的数据库中获取时间,格式始终为xx:xx:xx