- var_dump: documentation ( source)
- date_default_timezone_set: documentation ( source)
<?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;
}