0

我希望将来自 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 
4

2 回答 2

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');
于 2013-04-22T18:21:25.390 回答
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}";
}

在每个地方使用此功能,只需在需要时调用该功能..谢谢

于 2013-07-17T08:06:13.633 回答