我一直在寻找几个小时的答案,但我找不到答案。
我正在写一个简单的script
。用户设置他们的工作开始和结束时间。因此,例如,有人在8:00到16:00之间工作。
我怎样才能减去这个时间,看看这个人工作了多长时间?
我正在尝试strtotime();
,但没有成功......
答案 0 :(得分:25)
以下更好一点:
$a = new DateTime('08:00'); $b = new DateTime('16:00'); $interval = $a->diff($b); echo $interval->format("%H");
这会给你带来不同的时间差异。
答案 1 :(得分:9)
如果您获得有效的日期字符串,可以使用:
$workingHours = (strtotime($end) - strtotime($start)) / 3600;
这将为您提供一个人工作的时间。
答案 2 :(得分:0)
另一种解决方案是遍历Unix时间戳整数值(以秒为单位)。
<?php
$start = strtotime('10-09-2019 12:01:00');
$end = strtotime('12-09-2019 13:16:00');
$hours = intval(($end - $start)/3600);
echo $hours.' hours'; //in hours
//If you want it in minutes, you can divide the difference by 60 instead
$mins = (int)(($end - $start) / 60);
echo $mins.' minutues'.'<br>';
?>
如果您的原始日期以Unix时间戳格式存储,则此解决方案会更好。