我已经寻找答案几个小时了,但是没有找到。
我正在编写一个简单的 脚本
。用户设置他们的工作开始和结束时间。例如,某人从8:00工作到16:00。
如何减去这段时间以查看此人工作的时长?
我尝试使用strtotime()
进行实验,但没有成功...
我已经寻找答案几个小时了,但是没有找到。
我正在编写一个简单的 脚本
。用户设置他们的工作开始和结束时间。例如,某人从8:00工作到16:00。
如何减去这段时间以查看此人工作的时长?
我尝试使用strtotime()
进行实验,但没有成功...
以下代码更加清晰明了:
$a = new DateTime('08:00'); $b = new DateTime('16:00'); $interval = $a->diff($b);
echo $interval->format("%H");
这将给出两个时间之间的小时差异。
$workingHours = (strtotime($end) - strtotime($start)) / 3600;
<?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时间戳格式存储,则此解决方案将更好。