doudandui1592 2014-01-21 09:46
浏览 79

PHP模拟夏令时

I have some code that handles dates and times. I think it's OK, but want to test that it works when the clocks go forwards for DST.

I can change the timezone using date_default_timezone_set('Europe/London');, and wondered if there is an easy to simulate being in DST without having to wait till the clocks change!

  • 写回答

2条回答 默认 最新

  • doulu1945 2014-01-21 09:56
    关注

    You don't provide details on what you want to test but I assume you have functions to do time-related stuff, e.g.:

    function setExpiryTime(DateTime $start, $minutes){
    }
    

    The obvious test is to provide input parameters that will make your code cross DST boundaries. You can either find such information in your favourite search engine or run a simple PHP snippet:

    <?php
    
    $timezone = new DateTimeZone('Europe/London');
    print_r( $timezone->getTransitions(mktime(0, 0, 0, 1, 1, date('Y')), mktime(0, 0, 0, 12, 31, date('Y'))) );
    
    Array
    (
        [0] => Array
            (
                [ts] => 1388530800
                [time] => 2013-12-31T23:00:00+0000
                [offset] => 0
                [isdst] => 
                [abbr] => GMT
            )
    
        [1] => Array
            (
                [ts] => 1396141200
                [time] => 2014-03-30T01:00:00+0000
                [offset] => 3600
                [isdst] => 1
                [abbr] => BST
            )
    
        [2] => Array
            (
                [ts] => 1414285200
                [time] => 2014-10-26T01:00:00+0000
                [offset] => 0
                [isdst] => 
                [abbr] => GMT
            )
    
    )
    

    Thus you can test:

    setExpiryTime(new DateTime('2014-03-30T00:55:00+0000'), 10);
    setExpiryTime(new DateTime('2014-10-26T00:55:00+0000'), 10);
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮