在PHP中添加时间结果不正确
我对添加不正确的时间有问题。 例子: 00:00+00:35=11:13 应该是00:35而不是11:13 这是我的上述代码:在PHP中添加时间结果不正确,php,time,add,Php,Time,Add,我对添加不正确的时间有问题。 例子: 00:00+00:35=11:13 应该是00:35而不是11:13 这是我的上述代码: echo date('H:i',$total).' + '.$telat2.' = '; if($telat2 == '00:00'){ $total = $total;} else{ $total = ($total) + strtotime($telat2);} echo date('H:i',$total).' '; 我希望这里的每个人都能帮
echo date('H:i',$total).' + '.$telat2.' = ';
if($telat2 == '00:00'){
$total = $total;}
else{
$total = ($total) + strtotime($telat2);}
echo date('H:i',$total).' ';
我希望这里的每个人都能帮助我
提前谢谢
更新1
我刚刚得到正确的代码!
代码如下:
$total_unix = strtotime(date('Y-m-d').' '.$total.':00');
$telat2_unix = strtotime(date('Y-m-d').' '.$telat2.':00');
$begin_day_unix = strtotime(date('Y-m-d').' 00:00:00');
$total = date('H:i', ($total_unix + ($telat2_unix - $begin_day_unix)));
我想知道这怎么会发生
有人能给我解释一下吗?为原始客户制作了一个工作代码:
<?php
function AddTime($telat1,$telat2)
{
$telat1 = date_parse_from_format("H:i", $telat1);
$telat2 = $timeToAdd;
$telat2 = date_parse_from_format("H:i", $telat2);
$totalHours = $telat1['hour'] + $telat2['hour'];
$totalMinutes = $telat1['minute'] + $telat2['minute'];
if($totalMinutes >= 60)
{
$totalHours += 1;
$totalMinutes -= 60;
}
if($totalHours >= 24)
{
$totalHours -= 24;
}
// Setting the leading zeros
if(strlen($totalMinutes) < 2)
{
$totalMinutes = "0" . $totalMinutes;
}
if(strlen($totalHours) < 2)
{
$totalHours = "0" . $totalHours;
}
$total = $totalHours . ":" . $totalMinutes;
return $total;
}
$timeNow = date('H:i'); // Base time
$timeToAdd = '12:12'; // 12 hours 12 minutes to be added to $timeNow
$total = AddTime($timeNow, $timeToAdd);
echo $total;
?>
在执行此代码之前,哪些值具有$total
和$telat2
?据我所见,您试图使用时间戳来执行此操作,除非您将完整日期附加到它们,否则几乎永远都不会产生所需的结果。如果您只是尝试使用任意时间,进行添加,然后手动将其分解为小时、分钟等。是的,Jon,我只想从日期范围中重述一个人迟到的次数。。所以,只需要一天一天地加上迟到的时间。。对不起,我的英语很差……)@michael记录的第一行包含$total=strotime('00:00')$telat2='00:35';更新1和我的解决方案一样有效。等等,等等,我必须对你的建议做些什么,我在这里有点困惑。。对不起……)我以为你的问题是如何将“H:I”格式的时间添加到另一个“H:I”格式的时间。你原来的问题没有日期要求。只是时间,我知道了,所以当我们必须在另一个日期格式的时间中添加日期格式的时间时,它需要的是日期,而不仅仅是时间,对吗?谢谢你的建议,伙计……)