我有3个变量存储时间,它们如下;
我可以计算开始时间和结束时间之间的差,尽管它从未考虑过休息时间(即使break变量返回了正确的值)
$total_hours_work_cal = ($end_time_name - $total_break_name);
为此计算;
但是它只返回05:00,而不是预期的04:30
有什么想法吗?我尝试了很多用户在Stack Overflow上建议的方法,尽管无济于事。
**编辑*
已编辑以包含更多代码;
$start_time_name = $_POST['start_time_name'];
$end_time_name = $_POST['end_time_name'];
$total_break_name = $_POST['total_break_name'];
$total_hours_work_cal = ($end_time_name - $total_break_name - $start_time_name)
* 60 * 60;
echo "BEFORE CONVERSION" . $total_hours_work_cal;
$total_hours_work_cal = gmdate("H:i:s", $total_hours_work_cal);
echo "AFTER CONVERSION" . $total_hours_work_cal;
<?php
/*
$total_hours_work_cal = ($end_time_name - $total_break_name);
The calculation for this would be;
End time = 10:00
Start Time = 05:00
Break Time = 00:30
*/
$start = new \DateTime('05:00:00');
$end = new \DateTime('10:00:00');
$break = new \DateInterval('PT30M');
$beginEndInterval = $end->diff($start);
$total = new \DateTime('00:00');
$totalComp = new \DateTime('00:00');
$total->add($beginEndInterval);
$total->add($break);
echo $totalComp->diff($total)->format('%H:%I'); // 04:30