Bug #80748 [Nab]: DateInterval wrongly counts DateTime->diff days

From: Date: Mon, 22 Feb 2021 09:35:22 +0000
Subject: Bug #80748 [Nab]: DateInterval wrongly counts DateTime->diff days
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-232313@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80748&edit=1 ID: 80748 Updated by: derick@php.net Reported by: palansher at outlook dot com Summary: DateInterval wrongly counts DateTime->diff days Status: Not a bug Type: Bug Package: Date/time related Operating System: ubuntu 18.04 PHP Version: Irrelevant Block user comment: N Private report: N New Comment: PHP's Date/Time support does not deal with leap seconds: https://derickrethans.nl/leap-seconds-and-what-to-do-with-them.html Previous Comments: ------------------------------------------------------------------------ [2021-02-16 20:04:54] requinix@php.net > does PHP7 consider leap years and leap seconds in DT functions: Leap years definitely, leap seconds I don't know. ------------------------------------------------------------------------ [2021-02-16 19:45:57] palansher at outlook dot com Thank you! I need only know the one thing before creating my own solution: does PHP7 consider leap years and leap seconds in DT functions: dt->diff, ->modify, ->add, ->sub if to use UTC for every date? ------------------------------------------------------------------------ [2021-02-16 17:40:04] requinix@php.net > Why a time zone affects these calculations at all? It is the same number of > seconds for a certain year in a certain place. DST and other clock changing events mean that some days are more than 86400 seconds long and some days are less. > Maybe you would so kind and advise me on a PHP tool to do this? I cannot give you suitable advice because (a) this is a bug tracker and (b) I don't know your application. If PHP's diff is not suitable for your needs then you may have to come up with your own. One common approach that might also be suitable for you is to count calendar days only. That algorithm is simple: number of days from the start to the end of its month + number of days in the (whole) months in between the start and end + the number of days at the end. Dates are hard, and there is no single solution that works for everyone. ------------------------------------------------------------------------ [2021-02-16 17:14:55] palansher at outlook dot com @requinix@php.net Thank you for a detailed explanation. The only thing I cannot understand is: where and how applicable dt->diff is? For example (in my case) I need only count the time difference (in whole seconds, days, weeks) independent from geography. in one city, during a 100-200 years period. Why a time zone affects these calculations at all? It is the same number of seconds for a certain year in a certain place. It is a very important and interesting question. Maybe you would so kind and advise me on a PHP tool to do this? My final goal is to design a countable week numbers system that is independent of geography. Like countable years. For example week number 1,2 ..53245. It will allow me to get the "minus three weeks" number (i.e.) from the current week just deducting =53245-3 even across the year border. The very frst week can be selected arbitrarily, i.e 1st Jan 1900 (Monday) or 1st Jan 1990 (Monday) . ISO weeks system (1-52(53)) is not suitable for that because in the case of ISO "week number 1" deducting "1" in PHP code we will get "0" but not the correct result: 52(53)'th week of the previous year. Maybe the Unix timestamps will give precise accuracy as the tool for whole weeks calculations? Thank you for your time! ------------------------------------------------------------------------ [2021-02-15 23:14:07] requinix@php.net > it seems that the reason can be the "leap seconds" 1. There have been 37 leap seconds. I don't see how that can turn into 43 seconds. 2. The first leap second was in 1972. If you diff 1900 to 1970 then the difference still occurs with 25604/25605 days. It is not leap seconds. > in the last code example I set UTC timezone, not Moscow. Actually no. In your code where you use setTimezone, what you do is create a DateTime *according to the default timezone* and then modify the date to use the new timezone. If you want PHP to parse a date string according to a certain timezone then you have to specify that timezone at the time of parsing: with createFromFormat. https://3v4l.org/mWIjk Note that 3v4l's default timezone is Europe/Amsterdam, which used unusual UTC offsets through the 1930s. (Of course UTC didn't actually exist until the 1960s, so accusing Amsterdam of doing something weird according to a standard that hadn't been developed yet would be unfair.) ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=80748 -- Edit this bug report at https://bugs.php.net/bug.php?id=80748&edit=1

« previous php.bugs (#232313) next »