note 102316 deleted from function.strtotime by danbrown
| From: | danbrown@php.net | Date: | Tue, 08 Feb 2011 15:13:15 +0000 |
| Subject: | note 102316 deleted from function.strtotime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176778@lists.php.net to get a copy of this message | ||
Note Submitter: Anders strand
----
Do not use PHPDateTime below!!
It will make mistakes, especially with regards to leap years. $y%4 is NOT a reliable way to check
for leap years. ($y%100 and $y%400 is involved aswell.)
I would replace the DateNextMonth function, if i needed it, with something like this:
function addMonth($orgdate)
{
$next_month = strtotime($orgdate . ' +1 month');
//If the month in orgdate has more days then the next month we want to use the last of the
next month for all of them.
$last_day_next_month = strtotime($orgdate . ' last day of next month');
return date('Y-m-d', min($next_month, $last_day_next_month));
}
Trust strtotime to take care of the handling of leapyears and such instead of trying yourself. You
will not make a better job. (I'm looking at you ash :) )
echo addMonth('2010-01-31');
Outputs:
2010-02-28.