与PHP一起添加持续时间

时间:2015-05-27 07:36:49

标签: php datetime time difference

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

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)

希望这一切都有意义,如果我需要更详细地解释一下,请告诉我。谢谢!

3 个答案:

答案 0 :(得分:-1)

添加3次你应该做

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

答案 1 :(得分:-1)

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

希望这会有所帮助......

答案 2 :(得分:-1)

快速而肮脏的示例自己动手方法:

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以获取人类可读的输出。