3v4l.org

run code in 200+ PHP versions simultaneously
<?php class DTBug { private $intervals = [ '1min' => null, '1day' => null, '1wk' => null, ]; private $inspectCalc; public function __construct($inspectStored=false, $inspectCalc=false) { $this->inspectCalc = $inspectCalc; $this->intervals['1min'] = new \DateInterval("PT1M"); $this->intervals['1day'] = new \DateInterval("P1D"); $this->intervals['1wk'] = new \DateInterval("P7D"); if ($inspectStored) { array_map(function($s) { var_export($s, true); }, $this->intervals); } } public function compare(DateTimeInterface $start, DateTimeInterface $end) { $diff = $start->diff($end, true); if ($this->inspectCalc) { var_export($diff, true); } if ($diff < $this->intervals['1min']) { $op = "less than a minute"; } elseif ($diff < $this->intervals['1day']) { $op = "between an minute and a day"; } elseif ($diff < $this->intervals['1wk']) { $op = "between a day and a week"; } else { $op = "greater than a week"; } $this->output($start, $end, $op); } private function output(DateTimeInterface $start, DateTimeInterface $end, $op) { echo $start->format("Y-m-d H:i:s")." is ".$op." from ".$end->format("Y-m-d H:i:s")."\n"; } } echo "\nTesting without looking at the intervals:\n"; $bug = new DTBug; $bug->compare(new DateTimeImmutable("2016-05-16 09:12:32"), new DateTimeImmutable("2016-05-16 09:13:35")); // 1min 3sec $bug->compare(new DateTimeImmutable("2016-05-16 09:12:32"), new DateTimeImmutable("2016-05-16 12:13:35")); // 3hrs 1min 3sec $bug->compare(new DateTimeImmutable("2016-05-16 09:12:32"), new DateTimeImmutable("2016-05-18 09:13:35")); // 2day 1min 3sec $bug->compare(new DateTimeImmutable("2016-05-16 09:12:32"), new DateTimeImmutable("2016-07-16 09:13:35")); // 2mon 1min 3sec echo "\nTesting whilst looking at the stored intervals:\n"; $debug = new DTBug(true); $debug->compare(new DateTimeImmutable("2016-05-16 09:12:32"), new DateTimeImmutable("2016-05-16 09:13:35")); // 1min 3sec $debug->compare(new DateTimeImmutable("2016-05-16 09:12:32"), new DateTimeImmutable("2016-05-16 12:13:35")); // 3hrs 1min 3sec $debug->compare(new DateTimeImmutable("2016-05-16 09:12:32"), new DateTimeImmutable("2016-05-18 09:13:35")); // 2day 1min 3sec $debug->compare(new DateTimeImmutable("2016-05-16 09:12:32"), new DateTimeImmutable("2016-07-16 09:13:35")); // 2mon 1min 3sec echo "\nTesting whilst looking at the calculated intervals only:\n"; $debug = new DTBug(false, true); $debug->compare(new DateTimeImmutable("2016-05-16 09:12:32"), new DateTimeImmutable("2016-05-16 09:13:35")); // 1min 3sec $debug->compare(new DateTimeImmutable("2016-05-16 09:12:32"), new DateTimeImmutable("2016-05-16 12:13:35")); // 3hrs 1min 3sec $debug->compare(new DateTimeImmutable("2016-05-16 09:12:32"), new DateTimeImmutable("2016-05-18 09:13:35")); // 2day 1min 3sec $debug->compare(new DateTimeImmutable("2016-05-16 09:12:32"), new DateTimeImmutable("2016-07-16 09:13:35")); // 2mon 1min 3sec echo "\nTesting whilst looking at the both intervals:\n"; $debug = new DTBug(true, true); $debug->compare(new DateTimeImmutable("2016-05-16 09:12:32"), new DateTimeImmutable("2016-05-16 09:13:35")); // 1min 3sec $debug->compare(new DateTimeImmutable("2016-05-16 09:12:32"), new DateTimeImmutable("2016-05-16 12:13:35")); // 3hrs 1min 3sec $debug->compare(new DateTimeImmutable("2016-05-16 09:12:32"), new DateTimeImmutable("2016-05-18 09:13:35")); // 2day 1min 3sec $debug->compare(new DateTimeImmutable("2016-05-16 09:12:32"), new DateTimeImmutable("2016-07-16 09:13:35")); // 2mon 1min 3sec
based on Bbvk3
Output for 5.5.8 - 7.3.1
Testing without looking at the intervals: 2016-05-16 09:12:32 is greater than a week from 2016-05-16 09:13:35 2016-05-16 09:12:32 is greater than a week from 2016-05-16 12:13:35 2016-05-16 09:12:32 is greater than a week from 2016-05-18 09:13:35 2016-05-16 09:12:32 is greater than a week from 2016-07-16 09:13:35 Testing whilst looking at the stored intervals: 2016-05-16 09:12:32 is less than a minute from 2016-05-16 09:13:35 2016-05-16 09:12:32 is less than a minute from 2016-05-16 12:13:35 2016-05-16 09:12:32 is less than a minute from 2016-05-18 09:13:35 2016-05-16 09:12:32 is less than a minute from 2016-07-16 09:13:35 Testing whilst looking at the calculated intervals only: 2016-05-16 09:12:32 is greater than a week from 2016-05-16 09:13:35 2016-05-16 09:12:32 is greater than a week from 2016-05-16 12:13:35 2016-05-16 09:12:32 is greater than a week from 2016-05-18 09:13:35 2016-05-16 09:12:32 is greater than a week from 2016-07-16 09:13:35 Testing whilst looking at the both intervals: 2016-05-16 09:12:32 is between an minute and a day from 2016-05-16 09:13:35 2016-05-16 09:12:32 is between an minute and a day from 2016-05-16 12:13:35 2016-05-16 09:12:32 is between a day and a week from 2016-05-18 09:13:35 2016-05-16 09:12:32 is greater than a week from 2016-07-16 09:13:35
Output for 5.5.0 - 5.5.7
Testing without looking at the intervals: Fatal error: DateTime::diff() must be derived from DateTimeImmutable::diff in Unknown on line 0
Process exited with code 255.