note 102316 deleted from function.strtotime by danbrown

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
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.
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.