note 102366 added to function.strtotime

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
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
----
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/102366
Del: integrated  -- https://master.php.net/note/delete/102366/integrated
Del: useless     -- https://master.php.net/note/delete/102366/useless
Del: bad code    -- https://master.php.net/note/delete/102366/bad+code
Del: spam        -- https://master.php.net/note/delete/102366/spam
Del: non-english -- https://master.php.net/note/delete/102366/non-english
Del: in docs     -- https://master.php.net/note/delete/102366/in+docs
Del: other reasons-- https://master.php.net/note/delete/102366
Reject      -- https://master.php.net/note/reject/102366
Search      -- https://master.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.