note 95213 added to function.strtotime
| From: | emig647atyahoo dot com at osu1 dot php dot net | Date: | Fri, 18 Dec 2009 20:23:53 +0000 |
| Subject: | note 95213 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-164121@lists.php.net to get a copy of this message | ||
Some have added their fixes for adding 1 month to a time. The problem I find with other fixes is if
you are on the 30th (10-30-09) of a month that isn't the last day and add one month to their
time, it goes to 1 day less than the last day of the previous month's day. (11-29-09).
Here is my fix for adding 1 month so it's equal to of the previous month day or the last day of
the month if it's less than the previous month day.
$anniversary = "2008-09-30";
$newChargeDate = strtotime($anniversary);
$newChargeDate = addRealMonth($newChargeDate);
print date("Y-m-j", $newChargeDate);
//////////////////////////////////////////////////////
// Get the last day of the month
function lastDayOfMonth($month = '', $year = '')
{
if (empty($month)) {
$month = date('m');
}
if (empty($year)) {
$year = date('Y');
}
$result = strtotime("{$year}-{$month}-01");
$result = strtotime('-1 second', strtotime('+1 month', $result));
return date('Y-m-d', $result);
}
function addRealMonth($timeStamp)
{
// Check if it's the end of the year and the month and year need to be changed
$tempMonth = date('m', $timeStamp);
$tempYear = date('Y', $timeStamp);
if($tempMonth == "12")
{
$tempMonth = 1;
$tempYear++;
}
else
$tempMonth++;
$newDate = lastDayOfMonth($tempMonth, $tempYear);
return strtotime($newDate);
}
This will output 2008-10-30. Or 2009-02-28 if the $anniversary is 2009-01-30. Hope this helps!
----
Server IP: 192.41.40.227
Probable Submitter: 96.253.166.248
----
Manual Page -- http://www.php.net/manual/en/function.strtotime.php
Edit -- https://master.php.net/note/edit/95213
Del: integrated -- https://master.php.net/note/delete/95213/integrated
Del: useless -- https://master.php.net/note/delete/95213/useless
Del: bad code -- https://master.php.net/note/delete/95213/bad+code
Del: spam -- https://master.php.net/note/delete/95213/spam
Del: non-english -- https://master.php.net/note/delete/95213/non-english
Del: in docs -- https://master.php.net/note/delete/95213/in+docs
Del: other reasons-- https://master.php.net/note/delete/95213
Reject -- https://master.php.net/note/reject/95213
Search -- https://master.php.net/manage/user-notes.php