2

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
Rupert
  • 1,629
  • 11
  • 23
  • My guess is that Zend_Date::DATETIME_FULL is a constant and thus cannot be changed anymore. Try `echo $oDate->get();` –  Jan 31 '12 at 19:46
  • `$oDate = new Zend_Date(); $oDate->setMonth(2); $oDate->setDay(15); $oDate->setYear(2012); echo $oDate->getDate();//Mar 15, 2012 12:00:00 AM` This is the output with getDate(). It should be February, shouldn't it? – Rupert Jan 31 '12 at 19:50
  • Does it work with [month names](http://stackoverflow.com/questions/1545737/set-zend-date-month-using-user-input) at least or if you use `->set(4, Zend_Date::MONTH);` instead? (Else I would go for bug report, if no expert answer shows up here.) – mario Jan 31 '12 at 19:57
  • I tried running your code on my own server and saw the expected output (Jan,Feb,Mar,Apr,May). What happens if you try $oDate->setMonth('April'); ? – Mr Griever Jan 31 '12 at 20:02
  • 1
    Did you really run the script exactly as shown? The way the times are gradually changing would perhaps suggest that you didn't? – Matt Gibson Jan 31 '12 at 20:04
  • I don't see a problem with the code. Assuming you are seeing erroneous output, we need some information about your environment. What version of PHP, Zend Framework are you running? CLI or CGI, Web Server, OS, etc.? – Mr Griever Jan 31 '12 at 20:11
  • Mac OSX 10.6.8 MAMP 2.0.5 PHP 5.3.6 ZF 1.11.11 – Rupert Jan 31 '12 at 20:19
  • This is very odd. I just ran the above code and now it is working. I has not been working all day and now it works. wtf. – Rupert Jan 31 '12 at 20:28
  • The way I ran this code originally was by doing them one at a time. That is why there are differences in the minutes. – Rupert Jan 31 '12 at 20:33

1 Answers1

1

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!

Joe Bowman
  • 536
  • 2
  • 8