Heavily based on python-dateutil/rrule
Some useful terms to understand the algorithms and variables naming:
yearday = day of the year, from 0 to 365 (on leap years) - date('z')
weekday = day of the week (ISO-8601), from 1 (MO) to 7 (SU) - date('N')
monthday = day of the month, from 1 to 31
wkst = week start, the weekday (1 to 7) which is the first day of week.
Default is Monday (1). In some countries it's Sunday (7).
weekno = number of the week in the year (ISO-8601)
CAREFUL with this bug: https://bugs.php.net/bug.php?id=62476
/** * @expectedException RuntimeException */ public function testHumanReadableRuntimeException() { $rrule = new RRule(array('freq' => 'daily', 'count' => 10, 'dtstart' => '2007-01-01')); $rrule->humanReadable(array('locale' => 'xx', 'fallback' => 'xx')); // the locales are correctly formatted, but not such file exist, so this should throw a RuntimeException }
public static function getHumanReadableFromPattern($start, $pattern) { $untilpos = strpos($pattern, 'UNTIL'); $startDateTime = new \DateTime($start); if ($untilpos !== false) { $time = $startDateTime->format('\\This\\Z'); $newpattern = substr_replace($pattern, $time, $untilpos + 6 + 8, 0); $rule = 'DTSTART;TZID=Etc/GMT:' . $startDateTime->format('Ymd\\THis') . ' RRULE:' . $newpattern; } else { $rule = 'DTSTART;TZID=Etc/GMT:' . $startDateTime->format('Ymd\\THis') . ' RRULE:' . $pattern; } $rrule = new RRule($rule); return $rrule->humanReadable(array('locale' => 'fr')); }
/** * @dataProvider notOccurrences */ public function testNotOccurrences($rule, $not_occurences) { $rule = new RRule($rule); foreach ($not_occurences as $date) { $this->assertFalse($rule->occursAt($date), "Rule must not match {$date}"); } }
/** * Simple wrapper function to convert the RRule into its human readable form. * * @return string */ public function getRruleHumanReadable() { if ($this->rrule) { $rrule_str = $this->rrule; // ensure rrule string has a start date if not defined so that it will be part of the // formatted output if (!$this->isNewRecord && !strpos($rrule_str, 'DTSTART=')) { $created_date = new DateTime($this->created_date); $rrule_str .= ";DTSTART=" . $created_date->format('Y-m-d'); } $final_rrule = new RRule($rrule_str); return $final_rrule->humanReadable(array('date_formatter' => function ($d) { return $d->format(Helper::NHS_DATE_FORMAT); })); } }