PHP: Datetime :: Diff results comparison

I tried to compare the difference between the two dates, but it seems that the results are pretty wrong, for example, this code:

$datetime1 = new DateTime('2009-10-11'); $datetime2 = new DateTime('2009-10-13'); $interval = $datetime1->diff($datetime2); echo $interval->format('%R%a days')."<br />"; $datetime1 = new DateTime('2009-10-11'); $datetime2 = new DateTime('2009-10-15'); $interval2 = $datetime1->diff($datetime2); echo $interval2->format('%R%a days')."<br />"; if($interval == $interval2){ echo "true"; }else{echo "false"; } 

Returns true, but above you can see that the differences in the date do not match, in fact, the echo print is +2 and +4. Any idea comparing the two date differences?

EDIT: datetime :: diff returns a dateinterval object, in fact it does not implement comparison operators, https://bugs.php.net/bug.php?id=49914 I will use timinterval vars to check the difference, thanks for the answers

+6
source share
6 answers

DateInterval does not seem to implement a comparison function internally. Extensions are allowed to define their own comparison rules for their predefined classes. Obviously, he returns to the free comparison that objects are of the same class.

This function request contains a patch for adding this function, but it does not seem to have hit the source at any time.

To get around this problem, you can either compare each member variable of your objects yourself (years, months, etc.), or you can attribute each object to an array:

 if ((array) $interval == (array) $interval2) { echo 'true'; } else { echo 'false'; } 
+7
source

You only compare that two objects & shy; Docs have the same type (the same props value), but not that they are identical:

 if ($interval === $interval2) {echo "true";} else {echo "false";} ^^^ 

Note that you are comparing objects, not comparing values, for example, with a string.

+3
source

I have expanded the php class. Comparing methods makes comparing values. It uses the "natural" order of variables in the php DateInterval class. The foreach cycle goes first through years, then months, then days, etc. This is probably not a very portable solution, but it seems to work fine in php 5.3.

 /** * Description of DateInterval * * @author Santhos */ class DateInterval extends \DateInterval { /** * compares two date intervals * returns: * 0 - when they are equal * less than zero - $a is less than $b * greater than zero - $a is greater than $b * * @param \Designeo\Utils\DateInterval $dateInterval * @return int */ public static function compare($a, $b) { // check parameters self::isDateInterval($a); self::isDateInterval($b); foreach ($a as $key => $value) { // after seconds 's' comes 'invert' and other crap we do not care about // and it means that the date intervals are the same if ($key == 'invert') { return 0; } // when the values are the same we can move on if ($a->$key == $b->$key) { continue; } // finally a level where we see a difference, return accordingly if ($a->$key < $b->$key) { return -1; } else { return 1; } } } private static function isDateInterval($object) { if (!is_a($object, 'DateInterval')) { throw new \Exception('Parameter dateInterval type has to be a Dateinterval.'); } } public function compareToAnother($dateInterval) { return self::compare($this, $dateInterval); } } 
+2
source

You assign $ datetime1-> diff ($ datetime2) to both $ interval and $ interval2, so they have the same value

0
source

I remember that there is a date comparison function in php like this.

 compare_dates($start_date,$end_date); 
0
source

I made a comparison between two DateIntervals using the following method:

 version_compare(join('.', (array) $dateIntervalA), join('.', (array) $dateIntervalB)); 
0
source

Source: https://habr.com/ru/post/905070/


All Articles