Zend_Date 似乎不起作用

发布于 2025-01-01 16:29:11 字数 787 浏览 5 评论 0原文

我一直在使用 Zend_Date。谁能告诉我以下行为是否正常?返回的月份似乎是错误的。我已将输出放在评论中。

感谢您的帮助!

$oDate = new Zend_Date();

$oDate->setMonth(1);
$oDate->setDay(15);
$oDate->setYear(2012);
echo $oDate->get(Zend_Date::DATETIME_FULL);//Sunday, January 15, 2012 8:24:59 PM Europe/Madrid

$oDate->setMonth(2);
echo $oDate->get(Zend_Date::DATETIME_FULL);//Thursday, March 15, 2012 8:25:20 PM Europe/Madrid

$oDate->setMonth(3);
echo $oDate->get(Zend_Date::DATETIME_FULL);//Thursday, March 15, 2012 8:25:40 PM Europe/Madrid

$oDate->setMonth(4);
echo $oDate->get(Zend_Date::DATETIME_FULL);//Tuesday, May 15, 2012 8:27:32 PM Europe/Madrid

$oDate->setMonth(5);
echo $oDate->get(Zend_Date::DATETIME_FULL);//Tuesday, May 15, 2012 8:28:05 PM Europe/Madrid

I have been working with Zend_Date. Can anyone tell me if the behaviour below is normal? The returned months seem to be wrong. I have put the output in comments.

Thanks for any help!

$oDate = new Zend_Date();

$oDate->setMonth(1);
$oDate->setDay(15);
$oDate->setYear(2012);
echo $oDate->get(Zend_Date::DATETIME_FULL);//Sunday, January 15, 2012 8:24:59 PM Europe/Madrid

$oDate->setMonth(2);
echo $oDate->get(Zend_Date::DATETIME_FULL);//Thursday, March 15, 2012 8:25:20 PM Europe/Madrid

$oDate->setMonth(3);
echo $oDate->get(Zend_Date::DATETIME_FULL);//Thursday, March 15, 2012 8:25:40 PM Europe/Madrid

$oDate->setMonth(4);
echo $oDate->get(Zend_Date::DATETIME_FULL);//Tuesday, May 15, 2012 8:27:32 PM Europe/Madrid

$oDate->setMonth(5);
echo $oDate->get(Zend_Date::DATETIME_FULL);//Tuesday, May 15, 2012 8:28:05 PM Europe/Madrid

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我闹别瞎闹 2025-01-08 16:29:11

关键是您的原始发布日期。

在有 31 天的月份中,如果您在第 31 天并将月份设置为天数较少的月份(即二月、四月、六月、九月或十一月),则会将日期计算为该月的 31 日,并溢出到 1 日下个月的。

上面的脚本不是按原样运行,而是运行:

  $oDate->setMonth();
  $oDate->setDay();
  $oDate->setYear();
  echo ...

每个月,因此时间不同。

当设置为 4 月、6 月、9 月或 11 月时,该问题仅在给定月份的 31 日出现(因此当回复发布时,人们无法复制该问题);或将月份设置为二月时,每月的 29 日/30 日/31 日。

您可以在上面的示例中通过先设置日期,然后然后月份来解决此问题(因为月份不会溢出)。

当然,反过来你也会遇到同样的问题,即你在一个月内 << 31 天,并设置 days = 31,它会溢出到下个月的 1 号,这意味着您的日期将是正确月份的 1 号!

不幸的是,我还没有找到一个简单的解决方案(除了基于 PHP 的 DateTime 类编写一个新的日期类)。

希望这有帮助!

The key is your original post date.

In months that have 31 days, if you are on the 31st day and setMonth() to a month with less days (i.e. Feb, Apr, June, Sep or Nov), it calculates the date as 31st of that Month which overflows to 1st of the next month.

The script above wasn't run as is, but was run:

  $oDate->setMonth();
  $oDate->setDay();
  $oDate->setYear();
  echo ...

for each month, hence the differing times.

The problem only manifests itself on 31st of a given month (hence why when the replies were posted, people could not replicate the problem), when setting to Apr, June, Sept or Nov; or 29th/30th/31st of a month when setting month to Feb.

You can fix this in the examples above by setting the day first, then the month (as the month will not then overflow).

You of course get the same problem in reverse, whereby you are in a month with < 31 days, and set days = 31, which overflows to 1st of the following month, meaning your date will then be on the 1st of the correct month!

Unfortunately, I am yet to find a simple fix for this (other than writing a new date class based on PHP's DateTime class).

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文