note 102366 deleted from function.strtotime by danbrown
| From: | danbrown@php.net | Date: | Thu, 10 Feb 2011 16:16:37 +0000 |
| Subject: | note 102366 deleted from function.strtotime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176863@lists.php.net to get a copy of this message | ||
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