dongrandi8411 2014-10-08 15:50
浏览 26

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

Ok I have a function that counts the number of working hours between two dates and returns the time in minuets. Working Hours are between 08:30 and 17:30 excluding weekends.

This seems to be working fine on my development machine but when I put it on the live server for some reason the settime method jumps forward one hour in the middle of a loop.

Here is the code

$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."
";
    $startDate = new DateTime($start);
    $log .= 'start '. $startDate->format('Y-m-d H:i:s')."
";

    //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')."
";
        $startofday = clone $date;
        $startofday->setTime(8,30);
        $log .= 'start of day '. $startofday->format('Y-m-d H:i:s')."
";
        $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;
}

As you can see this code also adds a log file, the log file shows the discrepancy I am talking about.

Log file from my local machine:

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

Log file from the server:

 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

As you can see the 08:30:00 has suddenly became 09:30:00 after the first iteration of the loop. Why is this happening? Both servers are set to BST the current uk timezone.

The correct output should be

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

But because of this hour jump the server output is :

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

Removing 60 minuets.

Anybody got any ideas?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 AT89C51控制8位八段数码管显示时钟。
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口