note 95213 modified in function.strtotime by felipe
| From: | felipe@php.net | Date: | Sun, 20 Dec 2009 14:57:21 +0000 |
| Subject: | note 95213 modified in function.strtotime by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-164154@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.
<?php
$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!
--was--
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!
http://php.net/manual/en/function.strtotime.php