note 118382 added to datetime.diff
| From: | cagatay at devmonks dot net | Date: | Wed, 25 Nov 2015 15:20:36 +0000 |
| Subject: | note 118382 added to datetime.diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204229@lists.php.net to get a copy of this message | ||
Be careful using:
$date1 = new DateTime('now');
$date2 = new DateTime('tomorrow');
$interval = date_diff($date1, $date2);
echo $interval->format('In %a days');
In some situations, this won't say "in 1 days", but "in 0 days".
I think this is because "now" is the current time, while "tomorrow" is the
current day +1 but at a default time, lets say:
Now: 08:00pm, 01.01.2015
Tomorrow: 00:00am, 02.01.2015
In this case, the difference is not 24 hour, so it will says 0 days.
Better use "today", which should also use a default value like:
Today: 00:00am, 01.01.2015
Tomorrow: 00:00am, 02.01.2015
which now is 24 hour and represents 1 day.
This may sound logical and many will say "of course, this is right", but if you use it in
a naiv way (like I did without thinking), you can come to this moment and facepalm yourself.
Conclusion: "Now" is "Today", but in a different clock time, but still the same
day!
----
Server IP: 72.52.91.14
Probable Submitter: 213.95.133.22
----
Manual Page -- http://php.net/manual/en/datetime.diff.php
Edit -- https://master.php.net/note/edit/118382
Del: integrated -- https://master.php.net/note/delete/118382/integrated
Del: useless -- https://master.php.net/note/delete/118382/useless
Del: bad code -- https://master.php.net/note/delete/118382/bad+code
Del: spam -- https://master.php.net/note/delete/118382/spam
Del: non-english -- https://master.php.net/note/delete/118382/non-english
Del: in docs -- https://master.php.net/note/delete/118382/in+docs
Del: other reasons-- https://master.php.net/note/delete/118382
Reject -- https://master.php.net/note/reject/118382
Search -- https://master.php.net/manage/user-notes.php