note 120309 deleted from function.strtotime by peehaa
| From: | peehaa@php.net | Date: | Fri, 09 Dec 2016 21:35:54 +0000 |
| Subject: | note 120309 deleted from function.strtotime by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208300@lists.php.net to get a copy of this message | ||
Note Submitter: tgajewski007 at gmail dot com
----
addMonth solusion
$date = addMonth("2015-01-31", 1); // return 2015-02-28
$date = addMonth("2015-01-31", 3); // return 2015-04-30
$date = addMonth("2015-02-28", 1); // return 2015-03-28 (of corst :) )
function addMonth($date, $countOfMonth)
{
$day = date("d", $date);
$month = date("m", $date);
$year = date("Y", $date);
$dateOut = mktime(0, 0, 0, $month + $countOfMonth, $day, $year);
$monthOut = date("m", $dateOut);
$yearOut = date("Y", $dateOut);
if(($month + $countOfMonth) % 12 != $monthOut)
{
if($year != $yearOut)
{
$m = $month + $countOfMonth;
$m = $m % 12;
$lastDayOfMonth = cal_days_in_month(CAL_GREGORIAN, $m, $yearOut);
$dateOut = mktime(0, 0, 0, $m, $lastDayOfMonth, $yearOut);
}
else
{
$lastDayOfMonth = cal_days_in_month(CAL_GREGORIAN, $month + $countOfMonth, $year);
$dateOut = mktime(0, 0, 0, $month + $countOfMonth, $lastDayOfMonth, $year);
}
}
return date("Y-m-d", $dateOut);
}