我希望将来自sql查询的两次加在一起。以下是我的代码。
$taskID = $row['taskID'];
$startTimeRaw = $row['startTime'];
$endTimeRaw = $row['endTime'];
$everyMinutesRaw = $row['everyMinutes'];
$startTime = $startTimeRaw->format('H:i:s');
$endTime = $endTimeRaw->format('H:i:s');
$everyMinutes = $everyMinutesRaw->format('H:i:s');
#$latestRunTime = $startTime;
$latestRunTimeRaw = $startTime + $everyMinutes;
echo $startTime."<BR>";
echo $everyMinutes."<BR>";
echo $latestRunTime."<BR>";
此代码返回以下内容
06:05:00
00:15:00
6
返回的第三行应为06:20:00
,我该如何进行此更改。我玩过strtotime
和->format()
,但似乎都找不到合适的答案。想法?
根据其他答案中包含的数据我有这个
$latestRunTime = strtotime($startTime) + strtotime($everyMinutes);
并输出
2733243600
如果我格式化,我会得到以下
Fatal error: Call to a member function format() on a non-object in
答案 0 :(得分:1)
这似乎是你所追求的:
<?php
$period = new DatePeriod(
new DateTime('06:05:00'),
DateInterval::createFromDateString('15 minutes'),
new DateTime('07:00:00'));
foreach($period as $interval){
echo $interval->format('c').PHP_EOL;
}
结果:
2013-04-22T06:05:00+02:00
2013-04-22T06:20:00+02:00
2013-04-22T06:35:00+02:00
2013-04-22T06:50:00+02:00
您也可以使用new DateInterval('PT15M')
,或者更正式地使用
new DateInterval('PT'.$everyMinutesRaw->format('H\Hi\Ms\S'));
如果您对所有时间间隔不感兴趣,但只想按照您的示例选择第一个时间间隔:
$startTimeRaw->add(new DateInterval('PT'.$everyMinutesRaw->format('H\Hi\Ms\S')));
echo $startTimeRaw->format('H:i:s');
答案 1 :(得分:1)
使用此功能
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;
if($seconds < 9)
{
$seconds = "0".$seconds;
}
if($minutes < 9)
{
$minutes = "0".$minutes;
}
if($hours < 9)
{
$hours = "0".$hours;
}
return "{$hours}:{$minutes}:{$seconds}";
}
在您需要的任何地方使用此功能。谢谢