note 119227 added to dateinterval.format
| From: | zell1285 at gmail dot com | Date: | Fri, 22 Apr 2016 10:00:42 +0000 |
| Subject: | note 119227 added to dateinterval.format | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205303@lists.php.net to get a copy of this message | ||
I was doing this: $endDate->diff($startDate)->format('%aâ)
to get the number of the days between the end date and the start date. This is correct, but if I
subtract 2016-04-23 00:00:00 to 2016-04-02 09:39:01 (note that in only one of the datetime values we
have the time) the result is 21 (and not 20 as I expected). This is because the result of the diff
is 20 days AND 14 hours, 20 mins and 59 secs. If you use the %a to get the days from a dateinterval,
it will round the days (and if the hours are greater than 12, it will round days up).
So, DO NOT USE the %a formatting to get the difference in days between 2 datetime unless you are
100% sure that both the Datetime objects don't have the time.
You can solve easily with $valueDate->diff($startDate)->days that will return only the days in
a dateinterval (without the fuc***g rounding).
----
Server IP: 72.52.91.14
Probable Submitter: 79.173.168.75
----
Manual Page -- http://php.net/manual/en/dateinterval.format.php
Edit -- https://master.php.net/note/edit/119227
Del: integrated -- https://master.php.net/note/delete/119227/integrated
Del: useless -- https://master.php.net/note/delete/119227/useless
Del: bad code -- https://master.php.net/note/delete/119227/bad+code
Del: spam -- https://master.php.net/note/delete/119227/spam
Del: non-english -- https://master.php.net/note/delete/119227/non-english
Del: in docs -- https://master.php.net/note/delete/119227/in+docs
Del: other reasons-- https://master.php.net/note/delete/119227
Reject -- https://master.php.net/note/reject/119227
Search -- https://master.php.net/manage/user-notes.php