= 0` and `< 24`. * @throws Exception If the current scheduled period is **hourly** or if `$hour` is invalid. * @api */ public function setHour($hour) { if (!($hour >= 0 && $hour < 24)) { throw new Exception ("Invalid hour parameter, must be >=0 and < 24"); } $this->hour = $hour; } /** * By setting a timezone you make sure the scheduled task will be run at the requested time in the * given timezone. This is useful for instance in case you want to make sure a task runs at midnight in a website's * timezone. * * @param string $timezone */ public function setTimezone($timezone) { $this->timezone = $timezone; } protected function adjustTimezone($rescheduledTime) { if (is_null($this->timezone)) { return $rescheduledTime; } $arbitraryDateInUTC = Date::factory('2011-01-01'); $dateInTimezone = Date::factory($arbitraryDateInUTC, $this->timezone); $midnightInTimezone = date('H', $dateInTimezone->getTimestamp()); if ($arbitraryDateInUTC->isEarlier($dateInTimezone)) { $hoursDifference = 0 - $midnightInTimezone; } else { $hoursDifference = 24 - $midnightInTimezone; } $hoursDifference = $hoursDifference % 24; $rescheduledTime += (3600 * $hoursDifference); if ($this->getTime() > $rescheduledTime) { // make sure the rescheduled date is in the future $rescheduledTime = (24 * 3600) + $rescheduledTime; } return $rescheduledTime; } /** * Computes the delta in seconds needed to adjust the rescheduled time to the required hour. * * @param int $rescheduledTime The rescheduled time to be adjusted * @return int adjusted rescheduled time */ protected function adjustHour($rescheduledTime) { if ($this->hour !== null) { // Reset the number of minutes and set the scheduled hour to the one specified with setHour() $rescheduledTime = mktime($this->hour, 0, date('s', $rescheduledTime), date('n', $rescheduledTime), date('j', $rescheduledTime), date('Y', $rescheduledTime) ); } return $rescheduledTime; } /** * Returns a new ScheduledTime instance using a string description of the scheduled period type * and a string description of the day within the period to execute the task on. * * @param string $periodType The scheduled period type. Can be `'hourly'`, `'daily'`, `'weekly'`, or `'monthly'`. * @param bool|false|int|string $periodDay A string describing the day within the scheduled period to execute * the task on. Only valid for week and month periods. * * If `'weekly'` is supplied for `$periodType`, this should be a day * of the week, for example, `'monday'` or `'tuesday'`. * * If `'monthly'` is supplied for `$periodType`, this can be a numeric * day in the month or a day in one week of the month. For example, * `12`, `23`, `'first sunday'` or `'fourth tuesday'`. * @throws Exception * @api */ public static function factory($periodType, $periodDay = false) { switch ($periodType) { case 'hourly': return new Hourly(); case 'daily': return new Daily(); case 'weekly': $result = new Weekly(); if ($periodDay !== false) { $result->setDay($periodDay); } return $result; case 'monthly': $result = new Monthly($periodDay); if ($periodDay !== false) { if (is_int($periodDay)) { $result->setDay($periodDay); } else { $result->setDayOfWeekFromString($periodDay); } } return $result; default: throw new Exception("Unsupported scheduled period type: '$periodType'. Supported values are" . " 'hourly', 'daily', 'weekly' or 'monthly'."); } } }