NotionCommotion Posted August 23, 2020 Share Posted August 23, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/311372-how-to-compare-two-dateintervals/ Share on other sites More sharing options...
kicken Posted August 23, 2020 Share Posted August 23, 2020 Adding them to a date and comparing that seems fine. Otherwise you could inspect the various fields and compare those individually. Quote Link to comment https://forums.phpfreaks.com/topic/311372-how-to-compare-two-dateintervals/#findComment-1580910 Share on other sites More sharing options...
NotionCommotion Posted August 23, 2020 Author Share Posted August 23, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/311372-how-to-compare-two-dateintervals/#findComment-1580911 Share on other sites More sharing options...
requinix Posted August 23, 2020 Share Posted August 23, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311372-how-to-compare-two-dateintervals/#findComment-1580912 Share on other sites More sharing options...
NotionCommotion Posted August 24, 2020 Author Share Posted August 24, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311372-how-to-compare-two-dateintervals/#findComment-1580920 Share on other sites More sharing options...
requinix Posted August 24, 2020 Share Posted August 24, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311372-how-to-compare-two-dateintervals/#findComment-1580928 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.