简体   繁体   English

在php中找到两次之间的小时差不起作用

[英]finding hour difference between two times in php not working

i have 2 times in my sql table like 02:56:07pm and 03:56:14pm, so i converted it like:我的 sql 表中有 2 次,例如 02:56:07pm 和 03:56:14pm,所以我将其转换为:

 $time1 = date("G:i:s", strtotime($row['timein'])); $time2 = date("G:i:s", strtotime($row['timeout']));

which gives me two times like 14:56:07 and 15:56:14这给了我两次 14:56:07 和 15:56:14

now am trying to find the difference in hours between this two times and i did like below:现在我试图找到这两次之间的小时数差异,我喜欢以下内容:

 $difference = round(abs($time2 - $time1) / 3600,2); echo $difference;

but here am getting 0 as answer, can anyone please tell me how to fix it, thanks in advance但这里得到 0 作为答案,任何人都可以告诉我如何解决它,提前致谢

This works for me:这对我有用:

Using strtotime that converts a date/time string into a Unix Timestamp (eg. 163612416703).使用strtotime将日期/时间字符串转换为 Unix 时间戳(例如 163612416703)。 Now calculations can be done on the times.现在可以对时间进行计算。

$time1 = strtotime('02:56:07pm');
$time2 = strtotime('05:56:14pm');

$difference = date('H:i:s', ($time2 - $time1));
echo $difference; //03:00:07

Play around with the formatting etc..玩弄格式等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM