3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class Entry { private $modifier; private $startDateTime; private $untilDateTime; public function __construct($modifier, DateTime $startDateTime, DateTime $untilDateTime = null) { $this->setModifier($modifier); $this->setStartDateTime($startDateTime); if ($untilDateTime != null) { $this->setUntilDateTime($untilDateTime); } } public function setModifier($modifier) { $this->modifier = (string) $modifier; } public function getModifier() { return $this->modifier; } public function setStartDateTime(DateTime $startDateTime) { $this->startDateTime = $startDateTime; } public function getStartDateTime() { return clone $this->startDateTime; } public function setUntilDateTime(DateTime $untilDateTime) { $this->untilDateTime = $untilDateTime; } public function hasUntilDateTime() { return $this->untilDateTime instanceof DateTime; } public function getUntilDateTime() { return clone $this->untilDateTime; } public function match(DateTime $matchDateTime) { $currentDateTime = $this->getStartDateTime(); while ($currentDateTime <= $matchDateTime) { if ($currentDateTime->diff($matchDateTime)->days === 0) { return true; } if ($this->hasUntilDateTime() && $this->getUntilDateTime() < $currentDateTime) { break; } $currentDateTime->modify($this->getModifier()); } return false; } } date_default_timezone_set('America/Toronto'); $untilDateTime = new DateTime('2014-01-01'); $entries = [ new Entry('+1 month', new DateTime('2013-01-01')), new Entry('+2 week', new DateTime('2013-01-01')), new Entry('+3 day', new DateTime('2013-01-01')), ]; $todayDateTime = new DateTime('2013-01-01'); while ($todayDateTime < $untilDateTime) { var_dump($todayDateTime->format('Y-m-d')); foreach ($entries as $entry) { if ($entry->match($todayDateTime)) { var_dump($entry->getModifier()); } } $todayDateTime->modify('+1 day'); echo PHP_EOL; }
Output for 5.4.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
string(10) "2013-01-01" string(8) "+1 month" string(7) "+2 week" string(6) "+3 day" string(10) "2013-01-02" string(10) "2013-01-03" string(10) "2013-01-04" string(6) "+3 day" string(10) "2013-01-05" string(10) "2013-01-06" string(10) "2013-01-07" string(6) "+3 day" string(10) "2013-01-08" string(10) "2013-01-09" string(10) "2013-01-10" string(6) "+3 day" string(10) "2013-01-11" string(10) "2013-01-12" string(10) "2013-01-13" string(6) "+3 day" string(10) "2013-01-14" string(10) "2013-01-15" string(7) "+2 week" string(10) "2013-01-16" string(6) "+3 day" string(10) "2013-01-17" string(10) "2013-01-18" string(10) "2013-01-19" string(6) "+3 day" string(10) "2013-01-20" string(10) "2013-01-21" string(10) "2013-01-22" string(6) "+3 day" string(10) "2013-01-23" string(10) "2013-01-24" string(10) "2013-01-25" string(6) "+3 day" string(10) "2013-01-26" string(10) "2013-01-27" string(10) "2013-01-28" string(6) "+3 day" string(10) "2013-01-29" string(7) "+2 week" string(10) "2013-01-30" string(10) "2013-01-31" string(6) "+3 day" string(10) "2013-02-01" string(8) "+1 month" string(10) "2013-02-02" string(10) "2013-02-03" string(6) "+3 day" string(10) "2013-02-04" string(10) "2013-02-05" string(10) "2013-02-06" string(6) "+3 day" string(10) "2013-02-07" string(10) "2013-02-08" string(10) "2013-02-09" string(6) "+3 day" string(10) "2013-02-10" string(10) "2013-02-11" string(10) "2013-02-12" string(7) "+2 week" string(6) "+3 day" string(10) "2013-02-13" string(10) "2013-02-14" string(10) "2013-02-15" string(6) "+3 day" string(10) "2013-02-16" string(10) "2013-02-17" string(10) "2013-02-18" string(6) "+3 day" string(10) "2013-02-19" string(10) "2013-02-20" string(10) "2013-02-21" string(6) "+3 day" string(10) "2013-02-22" string(10) "2013-02-23" string(10) "2013-02-24" string(6) "+3 day" string(10) "2013-02-25" string(10) "2013-02-26" string(7) "+2 week" string(10) "2013-02-27" string(6) "+3 day" string(10) "2013-02-28" string(10) "2013-03-01" string(8) "+1 month" string(10) "2013-03-02" string(6) "+3 day" string(10) "2013-03-03" string(10) "2013-03-04" string(10) "2013-03-05" string(6) "+3 day" string(10) "2013-03-06" string(10) "2013-03-07" string(10) "2013-03-08" string(6) "+3 day" string(10) "2013-03-09" string(10) "2013-03-10" string(10) "2013-03-11" string(6) "+3 day" string(10) "2013-03-12" string(7) "+2 week" string(10) "2013-03-13" string(10) "2013-03-14" string(6) "+3 day" string(10) "2013-03-15" string(10) "2013-03-16" string(10) "2013-03-17" string(6) "+3 day" string(10) "2013-03-18" string(10) "2013-03-19" string(10) "2013-03-20" string(6) "+3 day" string(10) "2013-03-21" string(10) "2013-03-22" string(10) "2013-03-23" string(6) "+3 day" string(10) "2013-03-24" string(10) "2013-03-25" string(10) "2013-03-26" string(7) "+2 week" string(6) "+3 day" string(10) "2013-03-27" string(10) "2013-03-28" string(10) "2013-03-29" string(6) "+3 day" string(10) "2013-03-30" string(10) "2013-03-31" string(10) "2013-04-01" string(8) "+1 month" string(6) "+3 day" string(10) "2013-04-02" string(10) "2013-04-03" string(10) "2013-04-04" string(6) "+3 day" string(10) "2013-04-05" string(10) "2013-04-06" string(10) "2013-04-07" string(6) "+3 day" string(10) "2013-04-08" string(10) "2013-04-09" string(7) "+2 week" string(10) "2013-04-10" string(6) "+3 day" string(10) "2013-04-11" string(10) "2013-04-12" string(10) "2013-04-13" string(6) "+3 day" string(10) "2013-04-14" string(10) "2013-04-15" string(10) "2013-04-16" string(6) "+3 day" string(10) "2013-04-17" string(10) "2013-04-18" string(10) "2013-04-19" string(6) "+3 day" string(10) "2013-04-20" string(10) "2013-04-21" string(10) "2013-04-22" string(6) "+3 day" string(10) "2013-04-23" string(7) "+2 week" string(10) "2013-04-24" string(10) "2013-04-25" string(6) "+3 day" string(10) "2013-04-26" string(10) "2013-04-27" string(10) "2013-04-28" string(6) "+3 day" string(10) "2013-04-29" string(10) "2013-04-30" string(10) "2013-05-01" string(8) "+1 month" string(6) "+3 day" string(10) "2013-05-02" string(10) "2013-05-03" string(10) "2013-05-04" string(6) "+3 day" string(10) "2013-05-05" string(10) "2013-05-06" string(10) "2013-05-07" string(7) "+2 week" string(6) "+3 day" string(10) "2013-05-08" string(10) "2013-05-09" string(10) "2013-05-10" string(6) "+3 day" string(10) "2013-05-11" string(10) "2013-05-12" string(10) "2013-05-13" string(6) "+3 day" string(10) "2013-05-14" string(10) "2013-05-15" string(10) "2013-05-16" string(6) "+3 day" string(10) "2013-05-17" string(10) "2013-05-18" string(10) "2013-05-19" string(6) "+3 day" string(10) "2013-05-20" string(10) "2013-05-21" string(7) "+2 week" string(10) "2013-05-22" string(6) "+3 day" string(10) "2013-05-23" string(10) "2013-05-24" string(10) "2013-05-25" string(6) "+3 day" string(10) "2013-05-26" string(10) "2013-05-27" string(10) "2013-05-28" string(6) "+3 day" string(10) "2013-05-29" string(10) "2013-05-30" string(10) "2013-05-31" string(6) "+3 day" string(10) "2013-06-01" string(8) "+1 month" string(10) "2013-06-02" string(10) "2013-06-03" string(6) "+3 day" string(10) "2013-06-04" string(7) "+2 week" string(10) "2013-06-05" string(10) "2013-06-06" string(6) "+3 day" string(10) "2013-06-07" string(10) "2013-06-08" string(10) "2013-06-09" string(6) "+3 day" string(10) "2013-06-10" string(10) "2013-06-11" string(10) "2013-06-12" string(6) "+3 day" string(10) "2013-06-13" string(10) "2013-06-14" string(10) "2013-06-15" string(6) "+3 day" string(10) "2013-06-16" string(10) "2013-06-17" string(10) "2013-06-18" string(7) "+2 week" string(6) "+3 day" string(10) "2013-06-19" string(10) "2013-06-20" string(10) "2013-06-21" string(6) "+3 day" string(10) "2013-06-22" string(10) "2013-06-23" string(10) "2013-06-24" string(6) "+3 day" string(10) "2013-06-25" string(10) "2013-06-26" string(10) "2013-06-27" string(6) "+3 day" string(10) "2013-06-28" string(10) "2013-06-29" string(10) "2013-06-30" string(6) "+3 day" string(10) "2013-07-01" string(8) "+1 month" string(10) "2013-07-02" string(7) "+2 week" string(10) "2013-07-03" string(6) "+3 day" string(10) "2013-07-04" string(10) "2013-07-05" string(10) "2013-07-06" string(6) "+3 day" string(10) "2013-07-07" string(10) "2013-07-08" string(10) "2013-07-09" string(6) "+3 day" string(10) "2013-07-10" string(10) "2013-07-11" string(10) "2013-07-12" string(6) "+3 day" string(10) "2013-07-13" string(10) "2013-07-14" string(10) "2013-07-15" string(6) "+3 day" string(10) "2013-07-16" string(7) "+2 week" string(10) "2013-07-17" string(10) "2013-07-18" string(6) "+3 day" string(10) "2013-07-19" string(10) "2013-07-20" string(10) "2013-07-21" string(6) "+3 day" string(10) "2013-07-22" string(10) "2013-07-23" string(10) "2013-07-24" string(6) "+3 day" string(10) "2013-07-25" string(10) "2013-07-26" string(10) "2013-07-27" string(6) "+3 day" string(10) "2013-07-28" string(10) "2013-07-29" string(10) "2013-07-30" string(7) "+2 week" string(6) "+3 day" string(10) "2013-07-31" string(10) "2013-08-01" string(8) "+1 month" string(10) "2013-08-02" string(6) "+3 day" string(10) "2013-08-03" string(10) "2013-08-04" string(10) "2013-08-05" string(6) "+3 day" string(10) "2013-08-06" string(10) "2013-08-07" string(10) "2013-08-08" string(6) "+3 day" string(10) "2013-08-09" string(10) "2013-08-10" string(10) "2013-08-11" string(6) "+3 day" string(10) "2013-08-12" string(10) "2013-08-13" string(7) "+2 week" string(10) "2013-08-14" string(6) "+3 day" string(10) "2013-08-15" string(10) "2013-08-16" string(10) "2013-08-17" string(6) "+3 day" string(10) "2013-08-18" string(10) "2013-08-19" string(10) "2013-08-20" string(6) "+3 day" string(10) "2013-08-21" string(10) "2013-08-22" string(10) "2013-08-23" string(6) "+3 day" string(10) "2013-08-24" string(10) "2013-08-25" string(10) "2013-08-26" string(6) "+3 day" string(10) "2013-08-27" string(7) "+2 week" string(10) "2013-08-28" string(10) "2013-08-29" string(6) "+3 day" string(10) "2013-08-30" string(10) "2013-08-31" string(10) "2013-09-01" string(8) "+1 month" string(6) "+3 day" string(10) "2013-09-02" string(10) "2013-09-03" string(10) "2013-09-04" string(6) "+3 day" string(10) "2013-09-05" string(10) "2013-09-06" string(10) "2013-09-07" string(6) "+3 day" string(10) "2013-09-08" string(10) "2013-09-09" string(10) "2013-09-10" string(7) "+2 week" string(6) "+3 day" string(10) "2013-09-11" string(10) "2013-09-12" string(10) "2013-09-13" string(6) "+3 day" string(10) "2013-09-14" string(10) "2013-09-15" string(10) "2013-09-16" string(6) "+3 day" string(10) "2013-09-17" string(10) "2013-09-18" string(10) "2013-09-19" string(6) "+3 day" string(10) "2013-09-20" string(10) "2013-09-21" string(10) "2013-09-22" string(6) "+3 day" string(10) "2013-09-23" string(10) "2013-09-24" string(7) "+2 week" string(10) "2013-09-25" string(6) "+3 day" string(10) "2013-09-26" string(10) "2013-09-27" string(10) "2013-09-28" string(6) "+3 day" string(10) "2013-09-29" string(10) "2013-09-30" string(10) "2013-10-01" string(8) "+1 month" string(6) "+3 day" string(10) "2013-10-02" string(10) "2013-10-03" string(10) "2013-10-04" string(6) "+3 day" string(10) "2013-10-05" string(10) "2013-10-06" string(10) "2013-10-07" string(6) "+3 day" string(10) "2013-10-08" string(7) "+2 week" string(10) "2013-10-09" string(10) "2013-10-10" string(6) "+3 day" string(10) "2013-10-11" string(10) "2013-10-12" string(10) "2013-10-13" string(6) "+3 day" string(10) "2013-10-14" string(10) "2013-10-15" string(10) "2013-10-16" string(6) "+3 day" string(10) "2013-10-17" string(10) "2013-10-18" string(10) "2013-10-19" string(6) "+3 day" string(10) "2013-10-20" string(10) "2013-10-21" string(10) "2013-10-22" string(7) "+2 week" string(6) "+3 day" string(10) "2013-10-23" string(10) "2013-10-24" string(10) "2013-10-25" string(6) "+3 day" string(10) "2013-10-26" string(10) "2013-10-27" string(10) "2013-10-28" string(6) "+3 day" string(10) "2013-10-29" string(10) "2013-10-30" string(10) "2013-10-31" string(6) "+3 day" string(10) "2013-11-01" string(8) "+1 month" string(10) "2013-11-02" string(10) "2013-11-03" string(6) "+3 day" string(10) "2013-11-04" string(10) "2013-11-05" string(7) "+2 week" string(10) "2013-11-06" string(6) "+3 day" string(10) "2013-11-07" string(10) "2013-11-08" string(10) "2013-11-09" string(6) "+3 day" string(10) "2013-11-10" string(10) "2013-11-11" string(10) "2013-11-12" string(6) "+3 day" string(10) "2013-11-13" string(10) "2013-11-14" string(10) "2013-11-15" string(6) "+3 day" string(10) "2013-11-16" string(10) "2013-11-17" string(10) "2013-11-18" string(6) "+3 day" string(10) "2013-11-19" string(7) "+2 week" string(10) "2013-11-20" string(10) "2013-11-21" string(6) "+3 day" string(10) "2013-11-22" string(10) "2013-11-23" string(10) "2013-11-24" string(6) "+3 day" string(10) "2013-11-25" string(10) "2013-11-26" string(10) "2013-11-27" string(6) "+3 day" string(10) "2013-11-28" string(10) "2013-11-29" string(10) "2013-11-30" string(6) "+3 day" string(10) "2013-12-01" string(8) "+1 month" string(10) "2013-12-02" string(10) "2013-12-03" string(7) "+2 week" string(6) "+3 day" string(10) "2013-12-04" string(10) "2013-12-05" string(10) "2013-12-06" string(6) "+3 day" string(10) "2013-12-07" string(10) "2013-12-08" string(10) "2013-12-09" string(6) "+3 day" string(10) "2013-12-10" string(10) "2013-12-11" string(10) "2013-12-12" string(6) "+3 day" string(10) "2013-12-13" string(10) "2013-12-14" string(10) "2013-12-15" string(6) "+3 day" string(10) "2013-12-16" string(10) "2013-12-17" string(7) "+2 week" string(10) "2013-12-18" string(6) "+3 day" string(10) "2013-12-19" string(10) "2013-12-20" string(10) "2013-12-21" string(6) "+3 day" string(10) "2013-12-22" string(10) "2013-12-23" string(10) "2013-12-24" string(6) "+3 day" string(10) "2013-12-25" string(10) "2013-12-26" string(10) "2013-12-27" string(6) "+3 day" string(10) "2013-12-28" string(10) "2013-12-29" string(10) "2013-12-30" string(6) "+3 day" string(10) "2013-12-31" string(7) "+2 week"
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/TI4tN on line 65
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/TI4tN on line 65
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/TI4tN on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/TI4tN on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/TI4tN on line 5
Process exited with code 255.