我如何总结两个日期?
我有两个这种格式的日期 j h: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}";
}
?>
用法:
我所需要的只是将两个任务的两个日期相加,例如:
任务 1 需要 1 天 10 小时 20 分 10 秒
任务 2 将需要 3 天 17 小时 35 分 40 秒
所以结果将是任务 1 和任务 2 之间的总时间
最佳答案
只需使用 strtotime 转换日期并对时间戳求和,然后使用 date 函数再次转换为日期:
$tmstamp = strtotime($date1) + strtotime($date2);
echo date("Y m d", $tmstamp) ;
但是为什么要首先添加两个日期?
关于php - PHP中两个日期的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7343499/