Welcome! Log In Create A New Profile

Advanced

[PHP] Strange Result of Datetime::diff

Posted by Anonymous User 
Anonymous User
[PHP] Strange Result of Datetime::diff
April 03, 2017 02:50PM
Hi,

I'd expect the following script:

<?php

$date1 = new DateTime('2017-03-01');
$date2 = new DateTime('2017-04-02');

$diff = $date1->diff($date2);

echo "{$diff->m} months and {$diff->d} days.";
?>

would print "1 months and 0 days.". But instead it prints "1 months and 3 days." (see https://3v4l.org/RMuUZ).
What is the reason for this behaviour?

Thanks in advance and best regards
Christian
-------------------------------------------------------------------------------------------------
FreeMail powered by mail.de - MEHR SICHERHEIT, SERIOSITÄT UND KOMFORT

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
Re: [PHP] Strange Result of Datetime::diff
April 03, 2017 03:30PM
On Mon, Apr 3, 2017 at 8:39 AM, <[email protected]> wrote:

> Hi,
>
> I'd expect the following script:
>
> <?php
>
> $date1 = new DateTime('2017-03-01');
> $date2 = new DateTime('2017-04-02');
>
> $diff = $date1->diff($date2);
>
> echo "{$diff->m} months and {$diff->d} days.";
> ?>
>
> would print "1 months and 0 days.". But instead it prints "1 months and 3
> days." (see https://3v4l.org/RMuUZ).
> What is the reason for this behaviour?
>
> Thanks in advance and best regards
> Christian
> ------------------------------------------------------------
> -------------------------------------
> FreeMail powered by mail.de - MEHR SICHERHEIT, SERIOSITÄT UND KOMFORT
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
You sure about that, the above should yield 1 months and 1 days since you
have an extra day there:

http://sandbox.onlinephpfunctions.com/code/fe7c7d989101ab5e1dd1530d457bfcc7a1cfa8fa

If it was:

$date1 = new DateTime('2017-03-01');
$date2 = new DateTime('2017-04-01');

Then it would be exactly 1 month.
Anonymous User
Re: [PHP] Strange Result of Datetime::diff
April 03, 2017 05:30PM
Am 03-Apr-2017 15:28:44 +0200 schrieb azizsaleh@gmail.com:
>
> On Mon, Apr 3, 2017 at 8:39 AM, <[email protected]> wrote:
> > Hi,
> >
> > I'd expect the following script:
> >
> > <?php
> >
> > $date1 = new DateTime('2017-03-01');
> > $date2 = new DateTime('2017-04-02');
> >
> > $diff = $date1->diff($date2);
> >
> > echo "{$diff->m} months and {$diff->d} days.";
> > ?>
> >
> > would print "1 months and 0 days.". But instead it prints "1 months and 3 days." (see https://3v4l.org/RMuUZ).
> > What is the reason for this behaviour?
> >
> > Thanks in advance and best regards
> > Christian
> > -------------------------------------------------------------------------------------------------
> > FreeMail powered by mail.de - MEHR SICHERHEIT, SERIOSITÄT UND KOMFORT
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
>
> You sure about that, the above should yield 1 months and 1 days since you have an extra day there:
>
> http://sandbox.onlinephpfunctions.com/code/fe7c7d989101ab5e1dd1530d457bfcc7a1cfa8fa
>
> If it was:
>
> $date1 = new DateTime('2017-03-01');
> $date2 = new DateTime('2017-04-01');
>
> Then it would be exactly 1 month.
>

Aaw, of course you are right. I changed the code for another test and forgot to revert the change when I copied it into the mail.

So it seems like there is a bug. But what makes me wonder is that HHVM prints out the same result. That's why I wanted to ask this mailing list as I assume that I am missing something :-/

Does anyone have an idea?

Best regards
Christian
-------------------------------------------------------------------------------------------------
FreeMail powered by mail.de - MEHR SICHERHEIT, SERIOSITÄT UND KOMFORT

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ryan Pallas
Re: [PHP] Strange Result of Datetime::diff
April 03, 2017 05:40PM
On Apr 3, 2017 9:29 AM, <[email protected]> wrote:

Am 03-Apr-2017 15:28:44 +0200 schrieb azizsaleh@gmail.com:
>
> On Mon, Apr 3, 2017 at 8:39 AM, <[email protected]> wrote:
> > Hi,
> >
> > I'd expect the following script:
> >
> > <?php
> >
> > $date1 = new DateTime('2017-03-01');
> > $date2 = new DateTime('2017-04-02');
> >
> > $diff = $date1->diff($date2);
> >
> > echo "{$diff->m} months and {$diff->d} days.";
> > ?>
> >
> > would print "1 months and 0 days.". But instead it prints "1 months and
3 days." (see https://3v4l.org/RMuUZ).
> > What is the reason for this behaviour?
> >
> > Thanks in advance and best regards
> > Christian
> > ------------------------------------------------------------
-------------------------------------
> > FreeMail powered by mail.de - MEHR SICHERHEIT, SERIOSITÄT UND KOMFORT
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
>
> You sure about that, the above should yield 1 months and 1 days since you
have an extra day there:
>
> http://sandbox.onlinephpfunctions.com/code/fe7c7d989101ab5e1dd1530d457bfc
c7a1cfa8fa
>
> If it was:
>
> $date1 = new DateTime('2017-03-01');
> $date2 = new DateTime('2017-04-01');
>
> Then it would be exactly 1 month.
>

Aaw, of course you are right. I changed the code for another test and
forgot to revert the change when I copied it into the mail.

So it seems like there is a bug. But what makes me wonder is that HHVM
prints out the same result. That's why I wanted to ask this mailing list as
I assume that I am missing something :-/

Does anyone have an idea?


What time zone are you using? The difference may be related to daylight
savings/summer time as that changed in that specific period.


Best regards
Christian
------------------------------------------------------------
-------------------------------------
FreeMail powered by mail.de - MEHR SICHERHEIT, SERIOSITÄT UND KOMFORT

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph M. Becker
Re: [PHP] Strange Result of Datetime::diff
April 03, 2017 05:50PM
On 03.04.2017 at 15:28, Aziz Saleh wrote:

> On Mon, Apr 3, 2017 at 8:39 AM, <[email protected]> wrote:
>
>> I'd expect the following script:
>>
>> <?php
>>
>> $date1 = new DateTime('2017-03-01');
>> $date2 = new DateTime('2017-04-02');
>>
>> $diff = $date1->diff($date2);
>>
>> echo "{$diff->m} months and {$diff->d} days.";
>> ?>
>>
>> would print "1 months and 0 days.". But instead it prints "1 months and 3
>> days." (see https://3v4l.org/RMuUZ).
>> What is the reason for this behaviour?
>
> You sure about that, the above should yield 1 months and 1 days since you
> have an extra day there:
>
> http://sandbox.onlinephpfunctions.com/code/fe7c7d989101ab5e1dd1530d457bfcc7a1cfa8fa

But see https://3v4l.org/N5fg7!

--
Christoph M. Becker




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Maciek Sokolewicz
Re: [PHP] Strange Result of Datetime::diff
April 03, 2017 06:30PM
On 3-4-2017 17:36, Ryan Pallas wrote:
> On Apr 3, 2017 9:29 AM, <[email protected]> wrote:
>
> Am 03-Apr-2017 15:28:44 +0200 schrieb azizsaleh@gmail.com:
>>
>> On Mon, Apr 3, 2017 at 8:39 AM, <[email protected]> wrote:
>>> Hi,
>>>
>>> I'd expect the following script:
>>>
>>> <?php
>>>
>>> $date1 = new DateTime('2017-03-01');
>>> $date2 = new DateTime('2017-04-02');
>>>
>>> $diff = $date1->diff($date2);
>>>
>>> echo "{$diff->m} months and {$diff->d} days.";
>>> ?>
>>>
>>> would print "1 months and 0 days.". But instead it prints "1 months and
> 3 days." (see https://3v4l.org/RMuUZ).
>>> What is the reason for this behaviour?
>>>
>>> Thanks in advance and best regards
>>> Christian
>>> ------------------------------------------------------------
> -------------------------------------
>>> FreeMail powered by mail.de - MEHR SICHERHEIT, SERIOSITÄT UND KOMFORT
>>>
>>> --
>>> PHP General Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>
>>
>> You sure about that, the above should yield 1 months and 1 days since you
> have an extra day there:
>>
>> http://sandbox.onlinephpfunctions.com/code/fe7c7d989101ab5e1dd1530d457bfc
> c7a1cfa8fa
>>
>> If it was:
>>
>> $date1 = new DateTime('2017-03-01');
>> $date2 = new DateTime('2017-04-01');
>>
>> Then it would be exactly 1 month.
>>
>
> Aaw, of course you are right. I changed the code for another test and
> forgot to revert the change when I copied it into the mail.
>
> So it seems like there is a bug. But what makes me wonder is that HHVM
> prints out the same result. That's why I wanted to ask this mailing list as
> I assume that I am missing something :-/
>
> Does anyone have an idea?
>
>
> What time zone are you using? The difference may be related to daylight
> savings/summer time as that changed in that specific period.
>
>
> Best regards
> Christian
> ------------------------------------------------------------
> -------------------------------------
> FreeMail powered by mail.de - MEHR SICHERHEIT, SERIOSITÄT UND KOMFORT
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

I've had a bit of a test, and I believe it has to do with timezone handling.

I tested it by moving the dates 1 day forward (march 2nd till april
2nd), and then suddenly it changes back to a difference of 1 month and 0
days.

Let's assume we use current timezones; I'm in CET, so let's assume that
as the local timezone.

We input the following times:
2017-03-01 00:00:00 CET (UTC+1)
2017-04-01 00:00:00 CEST (UTC+2)

Converting the dates back to UTC times would yield:
2017-02-28 23:00:00 UTC
2017-03-31 22:00:00 UTC

So, we would now see a difference of 1 month and 3 days, which is given
as the answer by the diff function.

In other words, it seems to have to do with Timezone handling. If you
set the default timezone to UTC, the effect we see wil disappear.

Proof: https://3v4l.org/uoioc

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Anonymous User
Re: [PHP] Strange Result of Datetime::diff
April 04, 2017 12:20PM
Am 03-Apr-2017 18:22:16 +0200 schrieb tularis@php.net:
> On 3-4-2017 17:36, Ryan Pallas wrote:
> > On Apr 3, 2017 9:29 AM, <[email protected]> wrote:
> >
> > Am 03-Apr-2017 15:28:44 +0200 schrieb azizsaleh@gmail.com:
> >>
> >> On Mon, Apr 3, 2017 at 8:39 AM, <[email protected]> wrote:
> >>> Hi,
> >>>
> >>> I'd expect the following script:
> >>>
> >>> <?php
> >>>
> >>> $date1 = new DateTime('2017-03-01');
> >>> $date2 = new DateTime('2017-04-02');
> >>>
> >>> $diff = $date1->diff($date2);
> >>>
> >>> echo "{$diff->m} months and {$diff->d} days.";
> >>> ?>
> >>>
> >>> would print "1 months and 0 days.". But instead it prints "1 months and
> > 3 days." (see https://3v4l.org/RMuUZ).
> >>> What is the reason for this behaviour?
> >>>
> >>> Thanks in advance and best regards
> >>> Christian
> >>> ------------------------------------------------------------
> > -------------------------------------
> >>> FreeMail powered by mail.de - MEHR SICHERHEIT, SERIOSITÄT UND KOMFORT
> >>>
> >>> --
> >>> PHP General Mailing List (http://www.php.net/)
> >>> To unsubscribe, visit: http://www.php.net/unsub.php
> >>>
> >>
> >> You sure about that, the above should yield 1 months and 1 days since you
> > have an extra day there:
> >>
> >> http://sandbox.onlinephpfunctions.com/code/fe7c7d989101ab5e1dd1530d457bfc
> > c7a1cfa8fa
> >>
> >> If it was:
> >>
> >> $date1 = new DateTime('2017-03-01');
> >> $date2 = new DateTime('2017-04-01');
> >>
> >> Then it would be exactly 1 month.
> >>
> >
> > Aaw, of course you are right. I changed the code for another test and
> > forgot to revert the change when I copied it into the mail.
> >
> > So it seems like there is a bug. But what makes me wonder is that HHVM
> > prints out the same result. That's why I wanted to ask this mailing list as
> > I assume that I am missing something :-/
> >
> > Does anyone have an idea?
> >
> >
> > What time zone are you using? The difference may be related to daylight
> > savings/summer time as that changed in that specific period.
> >
> >
> > Best regards
> > Christian
> > ------------------------------------------------------------
> > -------------------------------------
> > FreeMail powered by mail.de - MEHR SICHERHEIT, SERIOSITÄT UND KOMFORT
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
>
> I've had a bit of a test, and I believe it has to do with timezone handling.
>
> I tested it by moving the dates 1 day forward (march 2nd till april
> 2nd), and then suddenly it changes back to a difference of 1 month and 0
> days.
>
> Let's assume we use current timezones; I'm in CET, so let's assume that
> as the local timezone.
>
> We input the following times:
> 2017-03-01 00:00:00 CET (UTC+1)
> 2017-04-01 00:00:00 CEST (UTC+2)
>
> Converting the dates back to UTC times would yield:
> 2017-02-28 23:00:00 UTC
> 2017-03-31 22:00:00 UTC
>
> So, we would now see a difference of 1 month and 3 days, which is given
> as the answer by the diff function.
>
> In other words, it seems to have to do with Timezone handling. If you
> set the default timezone to UTC, the effect we see wil disappear.
>
> Proof: https://3v4l.org/uoioc
>

Hi Maciek,

you are right. that helps me. Thanks a lot.

Christian
-------------------------------------------------------------------------------------------------
FreeMail powered by mail.de - MEHR SICHERHEIT, SERIOSITÄT UND KOMFORT

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ivan Grigoriev
Re: [PHP] Strange Result of Datetime::diff
April 06, 2017 10:20PM
03.04.2017 18:44, Christoph M. Becker пишет:
> On 03.04.2017 at 15:28, Aziz Saleh wrote:
>
>> On Mon, Apr 3, 2017 at 8:39 AM, <[email protected]> wrote:
>>
>>> I'd expect the following script:
>>>
>>> <?php
>>>
>>> $date1 = new DateTime('2017-03-01');
>>> $date2 = new DateTime('2017-04-02');
>>>
>>> $diff = $date1->diff($date2);
>>>
>>> echo "{$diff->m} months and {$diff->d} days.";
>>> ?>
>>>
>>> would print "1 months and 0 days.". But instead it prints "1 months and 3
>>> days." (see https://3v4l.org/RMuUZ).
>>> What is the reason for this behaviour?
>>
>> You sure about that, the above should yield 1 months and 1 days since you
>> have an extra day there:
>>
>> http://sandbox.onlinephpfunctions.com/code/fe7c7d989101ab5e1dd1530d457bfcc7a1cfa8fa
>
> But see https://3v4l.org/N5fg7!
>
Please, read this: https://bugs.php.net/bug.php?id=52480.
DateTime::diff works with UTC timezone. So, it converts current DateTime
object to UTC timezone.

If you want to understand how it calculated, do
$date1->setTimeZone(new DateTimeZone('UTC'));
$date2->setTimeZone(new DateTimeZone('UTC'));
and watch what actual values are used in diff.

One of the ways to calculate difference is to use timezone UTC for
DateTime object
$date1 = new DateTime('2017-03-01', new DateTimeZone('UTC'));
It calculates difference without time shifts and DTS rules.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login