Jump to content

How to compare two DateIntervals


NotionCommotion

Recommended Posts

I thought I was able to determine which DateInterval is greater the same as done with DateTime, however, either this is not true or I am doing something wrong.  Please advise.  Thanks!

    private function compareDateInterval(\DateInterval $dateInterval1, \DateInterval $dateInterval2):bool
    {
        $greaterThan = $dateInterval1 > $dateInterval2?'true':'false';
        echo("test1: dateInterval1 > dateInterval2: $greaterThan\n");

        $dateTimeNow = new \DateTimeImmutable;
        $dateTime1 = $dateTimeNow->add($dateInterval1);
        $dateTime2 = $dateTimeNow->add($dateInterval2);
        $greaterThan = $dateTime1 > $dateTime2?'true':'false';
        echo("test2: dateTime1 > dateTime2: $greaterThan\n");

        $timestampNow = $dateTimeNow->getTimestamp();
        $timestamp1 = $dateTime1->getTimestamp();
        $timestamp2 = $dateTime2->getTimestamp();
        $greaterThan = $timestamp1 > $timestamp2?'true':'false';
        echo("test3: timestamp1 > timestamp2: $greaterThan\n");
        
        echo('$dateTimeNow => '.json_encode($dateTimeNow).PHP_EOL);
        echo('$dateInterval1 => '.json_encode($dateInterval1).PHP_EOL);
        echo('$dateInterval2 => '.json_encode($dateInterval2).PHP_EOL);
        echo("timestampNow: $timestampNow, timestamp1: $timestamp1, timestamp2: $timestamp2\n");
    }
<b>Warning</b>:  Cannot compare DateInterval objects in <b>/var/www/app/src/Tools/PointTransitionHistory.php</b> on line <b>136</b><br />
test1: dateInterval1 > dateInterval2: false
test2: dateTime1 > dateTime2: true
test3: timestamp1 > timestamp2: true
$dateTimeNow => {"date":"2020-08-23 15:06:24.454702","timezone_type":3,"timezone":"UTC"}
$dateInterval1 => {"y":0,"m":0,"d":2,"h":14,"i":0,"s":0,"f":0,"weekday":0,"weekday_behavior":0,"first_last_day_of":0,"invert":1,"days":2,"special_type":0,"special_amount":0,"have_weekday_relative":0,"have_special_relative":0}
$dateInterval2 => {"y":0,"m":0,"d":11,"h":15,"i":0,"s":0,"f":0,"weekday":0,"weekday_behavior":0,"first_last_day_of":0,"invert":1,"days":11,"special_type":0,"special_amount":0,"have_weekday_relative":0,"have_special_relative":0}
timestampNow: 1598195184, timestamp1: 1597971984, timestamp2: 1597190784

 

Link to comment
Share on other sites

23 minutes ago, kicken said:

Adding them to a date and comparing that seems fine.  Otherwise you could inspect the various fields and compare those individually.

I am pretty sure I was able to use comparison operators on a DateInterval using some previous PHP version, but can no longer do so with PHP7.4.  Probably best to always add to some date, and will do so.  Related https://bugs.php.net/bug.php?id=49914

Link to comment
Share on other sites

Logically speaking, not all DateIntervals are comparable. Some are, depending on what they represent and how they were created. But most aren't. Consider 30D vs. 1M: sometimes they're equal, sometimes the first is less than the second, and sometimes the second is less than the first.

Like kicken said, compare the intervals with respect to some relevant starting date. And I bet there will always be some relevant date you can use.

Link to comment
Share on other sites

Agree that some DateIntervals' duration are a function of time and therefore the difference between two is as well, and it is kind of misleading if PHP reported the value.

That being said, some times it is good information to have.  How long is a month or a year?  Easy enough, however, to add my own method to estimate it, so not a problem.

Do you know whether I was correct and that PHP once implemented the ability to use comparison operators on DateIntervals?  Not that I want to do so, and just wondering whether I just imagined it.

Link to comment
Share on other sites

6 hours ago, NotionCommotion said:

Do you know whether I was correct and that PHP once implemented the ability to use comparison operators on DateIntervals?  Not that I want to do so, and just wondering whether I just imagined it.

DateIntervals have never been comparable. What has been the case was that PHP didn't prohibit it, but that meant you got the default object comparison behavior: always false.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.