note 102366 deleted from function.strtotime by danbrown
[email protected] Thu, 10 Feb 2011 08:16:37 -0800
| Newsgroups | php.notes |
|---|---|
| Message-ID | <[email protected]> |
Note Submitter: Anders
----
PHPDateTime below is a great example of a bad way to use strtotime. Why try to keep track of days per month and leap years yourself when strtotime most likely will do a better job of it?
Doing add a month, but consider any date after the last of the next month as the last, could be done something like this if relying entirely on strtotime to handle leap years and days per month and such.
<?php
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));
}
?>
<?php
echo addMonth('2010-01-31');
?>
will output: 2010-02-28