note 122021 added to datetime.modify
| From: | ilyaplot at gmail dot com | Date: | Fri, 15 Dec 2017 10:05:48 +0000 |
| Subject: | note 122021 added to datetime.modify | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-209366@lists.php.net to get a copy of this message | ||
function subMonths(\Datetime $dateTime, int $months)
{
if ($invert = $months < 0) {
$months *= -1;
}
for ($i=0; $i<$months; $i++) {
$daysOfMonth = cal_days_in_month(CAL_GREGORIAN, $dateTime->format('m'),
$dateTime->format('Y'));
$dateTime->modify(($invert ? '-' : '+') . $daysOfMonth . ' day');
}
return $dateTime;
}
$dateTime = new \DateTime('2004-12-31');
echo $dateTime->modify('-3 month')->format('Y-m-d'); // 2004-10-01
echo subMonths($dateTime, -3)->format('Y-m-d'); // 2004-09-30
----
Server IP: 208.43.231.9
Probable Submitter: 109.238.244.74
----
Manual Page -- http://php.net/manual/en/datetime.modify.php
Edit -- https://master.php.net/note/edit/122021
Del: integrated -- https://master.php.net/note/delete/122021/integrated
Del: useless -- https://master.php.net/note/delete/122021/useless
Del: bad code -- https://master.php.net/note/delete/122021/bad+code
Del: spam -- https://master.php.net/note/delete/122021/spam
Del: non-english -- https://master.php.net/note/delete/122021/non-english
Del: in docs -- https://master.php.net/note/delete/122021/in+docs
Del: other reasons-- https://master.php.net/note/delete/122021
Reject -- https://master.php.net/note/reject/122021
Search -- https://master.php.net/manage/user-notes.php