简体   繁体   English

Zend_Date,ISO_8601,日期解析和本地系统时钟

[英]Zend_Date, ISO_8601, date parsing and local system clock

I have a strange problem with Zend_Date object. 我对Zend_Date对象有一个奇怪的问题。

It seems that setters perform different operations with different system clock dates. 似乎setter使用不同的系统时钟日期执行不同的操作。 Let's assume that system date is 28 January 2013, following code: 我们假设系统日期是2013年1月28日,代码如下:

$now=new Zend_Date(Zend_Date::ISO_8601);
$now->now();
echo '<br/>now: ' . $now->toString();
echo '<br/>now->day: ' . $now->get(Zend_Date::DAY);
echo '<br/>now->month: ' . $now->get(Zend_Date::MONTH);
echo '<br/>now->year: ' . $now->get(Zend_Date::YEAR);
$end=new Zend_Date('2013-02-25 14:23:34', Zend_Date::ISO_8601);
echo '<br/>end: ' . $end->toString();
$end->setHour('23')->setMinute('59')->setSecond('59')->setDay($now->get(Zend_Date::DAY))->setMonth($now->get(Zend_Date::MONTH))->setYear($now->get(Zend_Date::YEAR));
echo '<br/>endAfterSetters: ' . $end->toString();

will produce following output: 将产生以下输出:

now: 28-01-2013 14:04:28
now->day: 28
now->month: 01
now->year: 2013
end: 25-02-2013 14:23:34
endAfterSetters: 28-01-2013 23:59:59

But if you change system clock to 29 January 2013, output is different from expectations: 但如果您将系统时钟更改为2013年1月29日,则输出与预期不同:

now: 29-01-2013 14:07:22
now->day: 29
now->month: 01
now->year: 2013
end: 25-02-2013 14:23:34
endAfterSetters: 01-01-2013 23:59:59

Last output is 01-01 -2013 23:59:59, but should be 29-01 -2013 23:59:59 ! 最后输出是01-01 -2013 23:59:59,但应该是29-01 -2013 23:59:59!

It happens on PHP 5.3.2 and 5.3.16, Zend_Framework 10.7, latest Zend_Date 24880 version. 它发生在PHP 5.3.2和5.3.16,Zend_Framework 10.7,最新的Zend_Date 24880版本上。

Everyting worked fine in the past. 过去每件事都很好。

Any ideas why it happens? 任何想法为什么会发生?

PS: I have also found jquery datatime plugin malfunciton while using it at 29,30,31 January... But i will describe it in other question. PS:我在1月29,30,31使用它时也发现了jquery datatime插件故障...但我会在其他问题中描述它。

Remember that your setters are called in sequence. 请记住,按顺序调用您的setter。 So when you call setDay(29) you're telling it to change the date to 29th February 2013 , which isn't a valid date, so it's rolling that over to make it 1st March 2013 . 因此,当您致电setDay(29)您告诉它将日期更改为2013年2月29日 ,这不是一个有效的日期,所以它正在滚动到2013年3月1日 Then you call setMonth(1) , which changes the month to January, giving you 1st January 2013 . 然后你调用setMonth(1) ,它将月份改为1月,给你2013年1月1日

You can control this behaviour by passing the extend_month option to the Zend_Date constructor, see: http://framework.zend.com/manual/1.12/en/zend.date.overview.html#zend.date.options.extendmonth 您可以通过将extend_month选项传递给Zend_Date构造函数来控制此行为,请参阅: http ://framework.zend.com/manual/1.12/en/zend.date.overview.html#zend.date.options.extendmonth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM