与PHP一起添加时间持续时间



我有一个表单,允许人们输入不同长度的时间,例如:

input.time1 = "19:00"
input.time2 = "2:30"
input.time3 = "0:30"

我需要将这3次加在一起,然后从我拥有的基础值(21:00)中减去它。

$base = "21:00"

然后我需要这样写:

$total = $time1 + $time2 + $time3; //equals 22:00
$base  = "21:00"; // set value
$diff  = $base - $total; // 21:00 - 22:00 = -1:00(mm:ss)

希望这些都是有意义的,如果我需要更详细地解释什么,请让我知道。谢谢你!

你应该做

<?php
$time = "19:00";
$time2 = "2:30";
$time3 = "0:30";
$secs = strtotime($time2)-strtotime("00:00");
$secs1 = strtotime($time3)-strtotime("00:00");
$result = date("H:i",strtotime($time)+$secs+$secs1);
echo $result;

现在$result将具有附加值

从附加值

中减去另一个值
$base  = "21:00";
$TimeStart = strtotime($result);
$TimeEnd = strtotime($base);
$Difference = ($TimeEnd - $TimeStart);
echo gmdate("H:i", $Difference);

$base = strtotime('21:00');
$total = strtotime($time1)+strtotime($time2);+strtotime($time3)
$diff = date("H:i", strtotime("$total - $base"));

快速和肮脏的例子自己动手的方法:

function string_to_int($time) {
    list($minutes, $seconds) = explode(':', $time);
    return $minutes * 60 + $seconds;
}
function int_to_string($seconds) {
    return sprintf('%d:%02d', floor($seconds / 60), $seconds % 60);
}
$duration = string_to_int('19:00') + string_to_int('2:30') + string_to_int('0:30');
echo int_to_string($duration);

xx:yy转换为整秒的整数,将它们相加,然后将其格式化为xx:yy以供人类可读输出。

相关内容

  • 没有找到相关文章