PHP 将两个时间变量相加
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11720845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP add up two time variables
提问by Rakesh
In my PHP application I want to calculate the sum of two time variables. I am looking for something like this example.
在我的 PHP 应用程序中,我想计算两个时间变量的总和。我正在寻找类似这个例子的东西。
$time1 = 15:20:00;
$time2 = 00:30:00;
$time = $time1+$time2;
采纳答案by Sammaye
The best way to do this is most likely to use strtotime to convert them to timestamps and then do the adding together:
最好的方法是最有可能使用 strtotime 将它们转换为时间戳,然后将它们加在一起:
$o = strtotime($time1)+strtotime($time2);
$o = strtotime($time1)+strtotime($time2);
If I remember right strtotimedoes support this format.
如果我没记错的话strtotime确实支持这种格式。
Otherwise you will need to filter it out yourself.
否则你需要自己过滤掉它。
回答by luissquall
If the answer you expect is 15:50:00and you want to use strtotimeand datefunctions, you need to subtract the seconds $time1and $time2share when you transform them to unix timestamps:
如果您期望的答案是15:50:00并且您想要使用strtotime和date函数,则需要在将它们转换为 unix 时间戳时减去秒$time1和$time2共享:
$time1 = '15:20:00';
$time2 = '00:30:00';
$time = strtotime($time1) + strtotime($time2) - strtotime('00:00:00');
$time = date('H:i:s', $time);
回答by cha
Following answer does not return correct value. It is summing integers but not returned correct time.
以下答案不会返回正确的值。它正在对整数求和,但没有返回正确的时间。
$o = strtotime($time1)+strtotime($time2);
$o = strtotime($time1)+strtotime($time2);
I created a function to get calculated time as follows.
我创建了一个函数来获取计算时间,如下所示。
public function addTwoTimes($time1 = "00:00:00", $time2 = "00:00:00"){
$time2_arr = [];
$time1 = $time1;
$time2_arr = explode(":", $time2);
//Hour
if(isset($time2_arr[0]) && $time2_arr[0] != ""){
$time1 = $time1." +".$time2_arr[0]." hours";
$time1 = date("H:i:s", strtotime($time1));
}
//Minutes
if(isset($time2_arr[1]) && $time2_arr[1] != ""){
$time1 = $time1." +".$time2_arr[1]." minutes";
$time1 = date("H:i:s", strtotime($time1));
}
//Seconds
if(isset($time2_arr[2]) && $time2_arr[2] != ""){
$time1 = $time1." +".$time2_arr[2]." seconds";
$time1 = date("H:i:s", strtotime($time1));
}
return date("H:i:s", strtotime($time1));
}
回答by feeela
You could use the PHP 5.3 DateInterval:
您可以使用 PHP 5.3 DateInterval:
$timeInterval = DateInterval::createFromDateString( '15 hours + 20 minutes' );
$timeInterval2 = DateInterval::createFromDateString( '30 minutes' );
foreach( str_split( 'ymdhis' ) as $prop )
{
$timeInterval->$prop += $timeInterval2->$prop;
}
var_dump( $timeInterval->format( '%H:%i:%s' ) );
(How to add to DateInterval objects was explained here: How we can add two date intervals in PHP)
(这里解释了如何添加到 DateInterval 对象:如何在 PHP 中添加两个日期间隔)
回答by Patrick G
As far as I can tell, Sammaye's answer did't work out for me.
据我所知,Sammaye 的回答对我不起作用。
I needed to start the time I wanted to add with the start of the UNIX timestamp. This way, strtotimereturns the seconds that need to be added to the first time.
我需要以 UNIX 时间戳的开头来开始我想要添加的时间。这样,strtotime返回需要添加到第一次的秒数。
$time1 = "15:20:00";
$time2 = "1970-01-01 00:30:00";
$time = strtotime($time1) + (strtotime($time2) + 3600);
echo $time . "<br />";
echo date("H:i:s", $time);
回答by britter
Be sure to consult the mystic docs http://us1.php.net/strtotimefor additional things you can input into your functions :)
请务必查阅神秘文档http://us1.php.net/strtotime以了解您可以输入到函数中的其他内容:)
$today = time();
$tommorrow = strtotime("+1 days", $today);
$day_after_tomorrow = strtotime("+1 days", $tomorrow);
回答by Sumesh TG
Code:
代码:
$time1 = '15:20:00';
$time2 = '00:30:00';
$time = strtotime($time1)+strtotime($time2);
$sumtime = date("H:i:s",$time);

