note 113140 added to datetime.formats.relative
| From: | shkkmo at gmail dot com | Date: | Thu, 05 Sep 2013 00:37:33 +0000 |
| Subject: | note 113140 added to datetime.formats.relative | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196165@lists.php.net to get a copy of this message | ||
Here is a fairly simple function I wrote to avoid the issues with adding/subtracting months that has
been mentioned by cristinawithout and many others.
I needed this function to be able handle monthly billing cycles properly. It handles positive and
negative values for months. It also can be used to side step a similar issues for adding years on
leap days (e.g. strtotime('2012/2/29 +1 year') by passing in multiples of 12 (e.g.
addMonthsToTime(12, strtotime('2012/2/29'))
<?
function addMonthsToTime($numMonths = 1, $timeStamp = null){
$timeStamp === null and $timeStamp = time();//Default to the present
$newMonthNumDays = date('d',strtotime('last day of '.$numMonths.'
months', $timeStamp));//Number of days in the new month
$currentDayOfMonth = date('d',$timeStamp);
if($currentDayOfMonth > $newMonthNumDays){
$newTimeStamp = strtotime('-'.($currentDayOfMonth - $newMonthNumDays).' days
'.$numMonths.' months', $timeStamp);
} else {
$newTimeStamp = strtotime($numMonths.' months', $timeStamp);
}
return $newTimeStamp;
}
?>
----
Server IP: 69.147.83.199
Probable Submitter: 98.210.222.176
----
Manual Page -- http://php.net/manual/en/datetime.formats.relative.php
Edit -- https://master.php.net/note/edit/113140
Del: integrated -- https://master.php.net/note/delete/113140/integrated
Del: useless -- https://master.php.net/note/delete/113140/useless
Del: bad code -- https://master.php.net/note/delete/113140/bad+code
Del: spam -- https://master.php.net/note/delete/113140/spam
Del: non-english -- https://master.php.net/note/delete/113140/non-english
Del: in docs -- https://master.php.net/note/delete/113140/in+docs
Del: other reasons-- https://master.php.net/note/delete/113140
Reject -- https://master.php.net/note/reject/113140
Search -- https://master.php.net/manage/user-notes.php