/** * Set last errors. * * @param array $lastErrors * * @return void */ private static function setLastErrors(array $lastErrors) { static::$lastErrors = $lastErrors; }
/** * Returns new Date object formatted according to the specified format. The * method supports both strptime() formats and DateTime formats * * @param string $format any format supported by DateTime, or a format name configured * @param string $time string representing the time. * @param string|DateTimeZone $time timezone, if null the default timezone will be used * * @throws Exception if the timezone passed is not valid * @throws OutOfBoundsException if the input time passed is not valid * * @return bool|Date new Date instance, or false on failure */ public function createFromFormat($format = 'local', $time, $timezone = null) { // deal with the timezone passed if ($timezone !== null) { if (!$timezone instanceof DateTimeZone) { $timezone = new DateTimeZone($timezone); } } else { $timezone = static::defaultTimezone(); } // custom format pattern lookup if (array_key_exists($format, static::$patterns)) { $format = static::$patterns[$format]; } // do we have a possible strptime() format to work with? if (strpos($format, '%') !== false and $timestamp = strptime($time, $format)) { // convert it into a timestamp $timestamp = mktime($timestamp['tm_hour'], $timestamp['tm_min'], $timestamp['tm_sec'], $timestamp['tm_mon'] + 1, $timestamp['tm_mday'], $timestamp['tm_year'] + 1900); if ($timestamp === false or $timestamp['unparsed']) { throw new \OutOfBoundsException('Input was invalid.' . (PHP_INT_SIZE == 4 ? ' A 32-bit system only supports dates between 1901 and 2038.' : '')); } // coversion was made in UTC, and strptime() does timezones but no daylight savings, so we might need a correction here if (($delta = static::defaultTimezone()->getOffset(new DateTime("2013-01-01 12:00:00", $timezone))) !== 0) { $time += $delta; } $format = 'U'; } if ($datetime = DateTime::createFromFormat($format, $time, $timezone)) { // if we had a delta, we're working with UTC, so we have to do some adjusting again if (isset($delta)) { $delta = $timezone->getOffset($datetime); if ($delta < 0) { $delta = new DateInterval('PT' . abs($delta) . 'S'); $delta->invert = 1; } else { $delta = new DateInterval('PT' . $delta . 'S'); } $datetime->sub($delta); } $datetime = new static('@' . $datetime->getTimestamp(), $timezone); } // update any errors found in the last DateTime static call static::$lastErrors = DateTime::getLastErrors(); if (!$datetime) { return false; } return $datetime; }