DateTime,setTime方法向前跳了一个小时,最近怎么回事?

时间:2014-10-08 15:50:51

标签: php datetime

好的我有一个功能可以计算两个日期之间的工作小时数,并以小步数的形式返回时间。工作时间在08:30至17:30之间,不包括周末。

这似乎在我的开发机器上正常工作但是当我因为某种原因将它放在实时服务器上时,settime方法在循环中间向前跳了一个小时。

这是代码

$onHoldStart = '2014-10-07 17:09:00';
$onHoldEnd = '2014-10-08 14:36:18';
$onHoldHours = biss_hours($onHoldStart, $onHoldEnd);

echo $onHoldHours.'<br />';
echo date('Y-m-d H:i:s').'<br />';

system('date +%Z');


/**
 * Counts the number of working hours between two given dates
 * E.g. Hours between 08:30 and 17:30 excluding weekends.
 * Takes start date and end date (Y-m-d H:i:s)
 * 
 */
function biss_hours($start, $end){
    $logfile='customlog.log';
    $log='';
    $log .='raw start '.$start."\n";
    $startDate = new DateTime($start);
    $log .= 'start '. $startDate->format('Y-m-d H:i:s')."\n";

    //date time period starts counting from the same hour so add hour to start to compensate
    $startDate->add(new DateInterval('PT1H'));
    $endDate = new DateTime($end);
    $periodInterval = new DateInterval( "PT1H" );

    $period = new DatePeriod( $startDate, $periodInterval, $endDate );
    $count = 0;

    foreach($period as $date){
        $log .= 'DATE '. $date->format('Y-m-d H:i:s')."\n";
        $startofday = clone $date;
        $startofday->setTime(8,30);
        $log .= 'start of day '. $startofday->format('Y-m-d H:i:s')."\n";
        $endofday = clone $date;
        $endofday->setTime(17,30);

        if($date > $startofday && $date <= $endofday && !in_array($date->format('l'), array('Sunday','Saturday'))){
            $count++;
        }
    }
    $log .='count '.$count;
    $test = file_put_contents($logfile, $log);
    return ($count * 60) + biss_minutes($start, $end);

}


function biss_minutes($start, $end){
    $diff = strtotime($end) - strtotime($start);
    $in = intval(date('i',$diff));
    return $in;
}

正如您所看到的,此代码还添加了一个日志文件,日志文件显示了我所说的差异。

我本地计算机的日志文件:

raw start 2014-10-07 17:09:00
start of day 2014-10-07 08:30:00
DATE 2014-10-07 19:09:00
start of day 2014-10-07 08:30:00
DATE 2014-10-07 20:09:00
start of day 2014-10-07 08:30:00
DATE 2014-10-07 21:09:00
(Skip a few)
count 6

来自服务器的日志文件:

 raw start 2014-10-07 17:09:00
 start 2014-10-07 17:09:00
 DATE 2014-10-07 18:09:00
 start of day 2014-10-07 08:30:00
 DATE 2014-10-07 19:09:00
 start of day 2014-10-07 09:30:00
 DATE 2014-10-07 20:09:00
 start of day 2014-10-07 09:30:00
 DATE 2014-10-07 21:09:00
 start of day 2014-10-07 09:30:00
 DATE 2014-10-07 22:09:00
 start of day 2014-10-07 09:30:00
 DATE 2014-10-07 23:09:00
 start of day 2014-10-07 09:30:00
 DATE 2014-10-08 00:09:00
 start of day 2014-10-08 09:30:00
 DATE 2014-10-08 01:09:00
 start of day 2014-10-08 09:30:00
 count 5

正如你所看到的,在循环的第一次迭代之后,08:30:00突然变成了09:30:00。为什么会这样?两台服务器都设置为BST当前的英国时区。

正确的输出应为

387 2014-10-08 16:32:47 BST

但由于这一小时跳转服务器输出是:

327 2014-10-08 16:33:16 BST

删除60分钟。

有人有任何想法吗?

0 个答案:

没有答案