note 102316 added to function.strtotime
| From: | Andersstrand at osu1 dot php dot net | Date: | Tue, 08 Feb 2011 15:10:02 +0000 |
| Subject: | note 102316 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-176777@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 213.115.126.82
----
Manual Page -- http://www.php.net/manual/en/function.strtotime.php
Edit -- https://master.php.net/note/edit/102316
Del: integrated -- https://master.php.net/note/delete/102316/integrated
Del: useless -- https://master.php.net/note/delete/102316/useless
Del: bad code -- https://master.php.net/note/delete/102316/bad+code
Del: spam -- https://master.php.net/note/delete/102316/spam
Del: non-english -- https://master.php.net/note/delete/102316/non-english
Del: in docs -- https://master.php.net/note/delete/102316/in+docs
Del: other reasons-- https://master.php.net/note/delete/102316
Reject -- https://master.php.net/note/reject/102316
Search -- https://master.php.net/manage/user-notes.php