PHP’s DateTime built-in class vs. Zend_Date
If you ever wanted a reason not to use PHP’s built-in DateTime class or to lie awake wondering about your code that uses it, here’s a good one. I’ve not looked into why this happens but 31st October minus one month gives 1st October. It should give 30th September. Zend_Date works in this respect:
Here’s a session showing this in action:
bob@desktop:~$ php -a
Interactive shell
php > $m = new DateTime();
php > echo $m->format( 'r' );
Mon, 31 Oct 2011 09:39:12 +1300
php > echo $m->modify('-1 month')->format( 'r' );
Sat, 01 Oct 2011 09:39:12 +1300
*cries*
bob@desktop:~$ php -a
Interactive shell
php > $m = new Zend_Date();
php > echo $m;
Oct 31, 2011 9:47:50 AM
php > echo $m->sub( 1, Zend_Date::MONTH );
Sep 30, 2011 9:48:25 AM
*correct*