* As against the official cron-documentation, this implementation
* does not support using nicknames (prefixed by the '@' character).
*
+ * Notice: This class used `gmdate()`/`gmmktime()` in previous versions,
+ * but now utilized the `date()`/`mktime()` counter-parts, but with the
+ * timezone set to the value of the `TIMEZONE` option.
+ *
* @author Alexander Ebert
* @copyright 2001-2017 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @return integer
*/
public static function calculateNextExec($minute, $hour, $dom, $month, $dow, $timeBase = TIME_NOW) {
+ // using the native `date()` and `mktime()` functions is dangerous
+ // unless we explicitly set the correct timezone
+ $originalTimezone = date_default_timezone_get();
+ if ($originalTimezone !== TIMEZONE) {
+ date_default_timezone_set(TIMEZONE);
+ }
+
// initialize fields
self::$timeBase = $timeBase;
self::$result = [
self::calculateTime($values);
// return timestamp
- return gmmktime(
+ $timestamp = mktime(
self::$result['hour'],
self::$result['minute'],
1,
self::$result['day'],
self::$result['year']
);
+
+ // restore the original timezone
+ if ($originalTimezone !== TIMEZONE) {
+ date_default_timezone_set($originalTimezone);
+ }
+
+ return $timestamp;
}
/**
$timeBase = self::$timeBase;
if ($addAnDay) {
- $date = explode('.', gmdate("d.m.Y", $timeBase));
- $timeBase = gmmktime(0, 0, 1, $date[1], $date[0] + 1, $date[2]);
+ $date = explode('.', date("d.m.Y", $timeBase));
+ $timeBase = mktime(0, 0, 1, $date[1], $date[0] + 1, $date[2]);
}
- $day = gmdate('j', $timeBase);
- $month = gmdate('n', $timeBase);
- $year = gmdate('Y', $timeBase);
+ $day = date('j', $timeBase);
+ $month = date('n', $timeBase);
+ $year = date('Y', $timeBase);
// calculate month of next execution and if its not the current one reset previous calculations
$dateMonth = self::calculateMonth($month, $year, $values);
$month = $dateMonth['month'];
$year = $dateMonth['year'];
- $timeBase = gmmktime(0, 0, 1, $month, $day, $year);
+ $timeBase = mktime(0, 0, 1, $month, $day, $year);
if (!$addAnDay) {
self::calculateHour($values, $timeBase);
// calculate date of next execution based upon day of week
$dateDow = self::calculateDow($month, $year, $values, $day);
- $dateDowTimestamp = gmmktime(0, 0, 1, $dateDow['month'], $dateDow['day'], $dateDow['year']);
+ $dateDowTimestamp = mktime(0, 0, 1, $dateDow['month'], $dateDow['day'], $dateDow['year']);
// calculate date of next execution based upon day of month
$dateDom = self::calculateDom($month, $year, $values, $day);
- $dateDomTimestamp = gmmktime(0, 0, 1, $dateDom['month'], $dateDom['day'], $dateDom['year']);
+ $dateDomTimestamp = mktime(0, 0, 1, $dateDom['month'], $dateDom['day'], $dateDom['year']);
// pick the earlier date if both dom and dow are restricted
if (self::$domRestricted && self::$dowRestricted) {
// compare day, month and year whether we have to recalculate hour and minute
if (($day != self::$result['day']) || ($month != self::$result['month']) || ($year != self::$result['year'])) {
// calculate new time base
- $timeBase = gmmktime(0, 0, 1, self::$result['month'], self::$result['day'], self::$result['year']);
+ $timeBase = mktime(0, 0, 1, self::$result['month'], self::$result['day'], self::$result['year']);
self::calculateHour($values, $timeBase);
}
* @return array
*/
protected static function calculateDow($month, $year, array &$values, $day = 1) {
- $days = gmdate('t', gmmktime(0, 0, 1, $month, $day, $year));
+ $days = date('t', mktime(0, 0, 1, $month, $day, $year));
for ($i = $day; $i <= $days; $i++) {
// get dow
- $dow = gmdate('w', gmmktime(0, 0, 1, $month, $i, $year));
+ $dow = date('w', mktime(0, 0, 1, $month, $i, $year));
if (in_array($dow, $values['dow'])) {
return [
* @return array
*/
protected static function calculateDom($month, $year, array &$values, $day = 1) {
- $days = gmdate('t', gmmktime(0, 0, 1, $month, $day, $year));
+ $days = date('t', mktime(0, 0, 1, $month, $day, $year));
for ($i = $day; $i <= $days; $i++) {
if (in_array($i, $values['dom'])) {
$addAnDay = false;
// compare hour
- $hour = intval(gmdate('G', $timeBase));
+ $hour = intval(date('G', $timeBase));
$index = self::findKey($hour, $values['hour'], false);
if ($index === false) {
$index = self::findKey($hour, $values['hour']);
$minute = 0;
}
else {
- $minute = gmdate('i', $timeBase);
+ $minute = date('i', $timeBase);
}
$index = self::findKey($minute, $values['minute'], false);