为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
萧十郎 2023-04-28 15:58:49
我有 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;
查看完整描述

3 回答

?
慕田峪7331174

TA贡献1828条经验 获得超13个赞

function hoursandmins($time, $format = '%02d:%02d'){

    if ($time < 1) {

        return;

    }

    $hours = floor($time / 60);

    $minutes = ($time % 60);

    return sprintf($format, $hours, $minutes);

}


$start_time_name    = "05:00";

$end_time_name  = "10:00";

$total_break_name   = "30"; //in minutes


$start_time_name = strtotime($start_time_name);

$end_time_name   = 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 minutes

echo $total_hours_work_cal;


查看完整回答
反对 回复 2023-04-28
?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

/* 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


查看完整回答
反对 回复 2023-04-28
?
守着一只汪

TA贡献1872条经验 获得超3个赞

<?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


查看完整回答
反对 回复 2023-04-28
  • 3 回答
  • 0 关注
  • 131 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信