PHP - 无法从时间中删除分钟

我有 3 个变量存储时间,它们如下;

  • $start_time_name = 开始时间

  • $end_time_name = 结束时间

  • $total_break_name = 休息时间

我可以计算开始时间和结束时间之间的差异,尽管它从不考虑中断(即使中断变量带回正确的值)

$total_hours_work_cal = ($end_time_name - $total_break_name);

对此的计算是;

  • 结束时间 = 10:00

  • 开始时间 = 05:00

  • 休息时间 = 00:30

但它只返回 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;


萧十郎
浏览 132回答 3
3回答

慕田峪7331174

function hoursandmins($time, $format = '%02d:%02d'){&nbsp; &nbsp; if ($time < 1) {&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; $hours = floor($time / 60);&nbsp; &nbsp; $minutes = ($time % 60);&nbsp; &nbsp; return sprintf($format, $hours, $minutes);}$start_time_name&nbsp; &nbsp; = "05:00";$end_time_name&nbsp; = "10:00";$total_break_name&nbsp; &nbsp;= "30"; //in minutes$start_time_name = strtotime($start_time_name);$end_time_name&nbsp; &nbsp;= strtotime($end_time_name);$total_break_name = (int) $total_break_name * 60; //convert into seconds$total_hours_work_cal = $end_time_name - $total_break_name - $start_time_name; //total time will be in seconds$total_hours_work_cal = $total_hours_work_cal / 60; //converted into minutes;$total_hours_work_cal = hoursandmins($total_hours_work_cal); //convert into hours and minutesecho $total_hours_work_cal;

哈士奇WWW

/* justfor testing purpose to have input */$_POST['start_time_name'] = "05:00";$_POST['total_break_name'] = "00:30";$_POST['end_time_name'] = "10:00";$start_time_name = $_POST['start_time_name'];$end_time_name = $_POST['end_time_name'];$total_break_name = $_POST['total_break_name'];$end_time = mktime(explode(":",$end_time_name)[0],explode(":",$end_time_name)[1]);$start_time = mktime(explode(":",$start_time_name)[0],explode(":",$start_time_name)[1]);$total_break = mktime(explode(":",$total_break_name)[0],explode(":",$total_break_name)[1]);$diff = $end_time + $total_break - $start_time;$total_hours_work_cal = date("H:i",$diff);echo "TOTAL HOURS OF WORK : " . $total_hours_work_cal;// TOTAL HOURS OF WORK : 05:30

守着一只汪

<?php/*$total_hours_work_cal = ($end_time_name - $total_break_name);The calculation for this would be;&nbsp; &nbsp; End time = 10:00&nbsp; &nbsp; Start Time = 05:00&nbsp; &nbsp; 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
打开App,查看更多内容
随时随地看视频慕课网APP