我如何总结两个日期?
我有两个日期格式jh:i:s我想总结一下
我找到了这个脚本,但没有关于天的参考,我是新手,这是脚本,也许有人可以进行必要的修改.
<?php
/**
* @author Masino Sinaga, http://www.openscriptsolution.com
* @copyright October 13, 2009
*/
echo sum_the_time('01:45:22', '17:27:03'); // this will give you a result: 19:12:25
function sum_the_time($time1, $time2) {
$times = array($time1, $time2);
$seconds = 0;
foreach ($times as $time)
{
list($hour,$minute,$second) = explode(':', $time);
$seconds += $hour*3600;
$seconds += $minute*60;
$seconds += $second;
}
$hours = floor($seconds/3600);
$seconds -= $hours*3600;
$minutes = floor($seconds/60);
$seconds -= $minutes*60;
return "{$hours}:{$minutes}:{$seconds}";
}
?>
Run Code Online (Sandbox Code Playgroud)
用法:
我所需要的就是从两个任务中总结两个日期:任务1需要1天10小时20分10秒任务2需要3天17小时35分40秒
所以结果将是任务1和2之间的总时间
只使用strtotime转换日期并将时间戳相加,而不是使用日期函数再次转换为日期:
$tmstamp = strtotime($date1) + strtotime($date2);
echo date("Y m d", $tmstamp) ;
Run Code Online (Sandbox Code Playgroud)
但为什么你会在第一时间添加两个日期?