PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php-解决Zend日期DST错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

发现了a bug in Zend_Date when setting a time that crosses the DST change.

这是说明问题的代码

date_default_timezone_set('America/New_York');

echo '<pre>';

// DST BEGINS '2012-03-11 02:00:00' - "Spring ForWARD"
$a = new ZEND_DATE('2012-03-11 00:00:00', 'yyyy-MM-dd HH:mm:ss');
$a->setTime('04:00:00', 'HH:mm:ss');
echo $a->toString('yyyy-MM-dd HH:mm:ss', 'iso')
        . ' // expected: 2012-03-11 04:00:00' . PHP_EOL;
$b = new ZEND_DATE('2012-03-11 04:00:00', 'yyyy-MM-dd HH:mm:ss');
$b->setTime('00:00:00', 'HH:mm:ss');
echo $b->toString('yyyy-MM-dd HH:mm:ss', 'iso')
        . ' // expected: 2012-03-11 00:00:00' . PHP_EOL;

// DST ENDS '2012-11-04 02:00:00' - "fall BACk"
$c = new ZEND_DATE('2012-11-04 00:00:00', 'yyyy-MM-dd HH:mm:ss');
$c->setTime('04:00:00', 'HH:mm:ss');
echo $c->toString('yyyy-MM-dd HH:mm:ss', 'iso')
        . ' // expected: 2012-11-06 04:00:00' . PHP_EOL;
$d = new ZEND_DATE('2012-11-04 04:00:00', 'yyyy-MM-dd HH:mm:ss');
$d->setTime('00:00:00', 'HH:mm:ss');
echo $d->toString('yyyy-MM-dd HH:mm:ss', 'iso')
        . ' // expected: 2012-11-06 00:00:00' . PHP_EOL;

echo '</pre>';

2012-03-11 05:00:00 // expected: 2012-03-11 04:00:00
2012-03-10 23:00:00 // expected: 2012-03-11 00:00:00
2012-11-04 03:00:00 // expected: 2012-11-06 04:00:00
2012-11-04 01:00:00 // expected: 2012-11-06 00:00:00

我需要解决这个错误,我很困惑.

基于来自nerdzilathe answer和针对a bug introduced by his solution的研究,现在在我的子类ZEND_DATE中具有以下内容

/**
 * Call the function twice to work around the DST bug
 * http://zendframework.com/issues/browse/ZF-10584
 * https://stackoverflow.com/questions/7181702/work-around-for-zend-date-dst-bug
 * https://stackoverflow.com/questions/8593660/zend-date-dst-bug-test-whether-a-date-is-a-time-change-date
 * TODO: remove this once the bug is fixed
 *
 * @param  String|Integer|array|ZEND_DATE  $time    Time to set
 * @param  String                          $format  OPTIONAL Timeformat for parsing input
 * @param  String|Zend_Locale              $locale  OPTIONAL Locale for parsing input
 * @return My_Date Provides fluid interface
 * @throws ZEND_DATE_Exception
 */
public function setTime($time, $format = null, $locale = null)
{
    // start time zone juggling so that localtime() returns the correct results
    $tzOrig = date_default_timezone_get();
    date_default_timezone_set($this->getTimezone());

    // capture orignal info
    $timeInfoOrg = localtime($this->gettimestamp(), truE);

    // set the time
    parent::setTime($time, $format, $localE);

    // if the dst has changed, perform workaround
    $timeInfoNew = localtime($this->gettimestamp(), truE);
    if ((0 < $timeInfoOrg['tm_isdst']) != (0 < $timeInfoNew['tm_isdst'])) {
        // set the time again
        parent::setTime($time, $format, $localE);
        // if the day changed, set it BACk
        if ($timeInfoOrg['tm_yday'] != $timeInfoNew['tm_yday']) {
            // localtime() year date is zero indexed, add one
            $this->setDayOfYear($timeInfoOrg['tm_yday'] + 1);
        }
    }

    // end time zone juggling
    date_default_timezone_set($tzOrig);

    // fluent
    return $this;
}

错误修复后,我可以简单地将其@L_874_13@.

解决方法:

那是一个不幸的错误.我通过在每个日期两次调用setTime()来解决它:

...
$a->setTime('04:00:00', 'HH:mm:ss');
$a->setTime('04:00:00', 'HH:mm:ss');
...
$b->setTime('00:00:00', 'HH:mm:ss');
$b->setTime('00:00:00', 'HH:mm:ss');
...
$c->setTime('04:00:00', 'HH:mm:ss');
$c->setTime('04:00:00', 'HH:mm:ss');
...
$d->setTime('00:00:00', 'HH:mm:ss');
$d->setTime('00:00:00', 'HH:mm:ss');
...

我的结果:

2012-03-11 04:00:00 // expected: 2012-03-11 04:00:00
2012-03-10 00:00:00 // expected: 2012-03-11 00:00:00
2012-11-04 04:00:00 // expected: 2012-11-04 04:00:00
2012-11-04 00:00:00 // expected: 2012-11-04 00:00:00

大佬总结

以上是大佬教程为你收集整理的php-解决Zend日期DST错误全部内容,希望文章能够帮你解决php-解决Zend日期DST错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: