note 126892 added to datetime.diff
| From: | steve.portable.com.au via php-notes | Date: | Tue, 01 Mar 2022 23:12:35 +0000 |
| Subject: | note 126892 added to datetime.diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-217671@lists.php.net to get a copy of this message | ||
There is an interesting quirk around leap days. If the first date is a leap date (29th February in a
leap year) and the second date is not a leap year, the results may not be what you expect:
<?php
$tz = new DateTimeZone('UTC');
$dt1 = new DateTime('2012-02-29 13:00:00', $tz);
$dt2 = new DateTime('2013-03-01 12:00:00', $tz);
print $dt1->diff($dt2)->format('%r%y years, %m months, %d days, %h hours, %i minutes, %s
seconds');
/* Output:
0 years, 11 months, 30 days, 23 hours, 0 minutes, 0 seconds
*/
?>
If you reverse the order of the dates you get:
<?php
print $dt2->diff($dt1)->format('%r%y years, %m months, %d days, %h hours, %i minutes, %s
seconds');
/* Output:
-1 years, 0 months, 0 days, 23 hours, 0 minutes, 0 seconds
*/
?>
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.135 (proxied: 2001:8003:43b0:9e01:b8a3:4bf2:7da5:cc9f)
----
Manual Page -- https://php.net/manual/en/datetime.diff.php
Edit -- https://main.php.net/note/edit/126892
Del: integrated -- https://main.php.net/note/delete/126892/integrated
Del: useless -- https://main.php.net/note/delete/126892/useless
Del: bad code -- https://main.php.net/note/delete/126892/bad+code
Del: spam -- https://main.php.net/note/delete/126892/spam
Del: non-english -- https://main.php.net/note/delete/126892/non-english
Del: in docs -- https://main.php.net/note/delete/126892/in+docs
Del: other reasons-- https://main.php.net/note/delete/126892
Reject -- https://main.php.net/note/reject/126892
Search -- https://main.php.net/manage/user-notes.php