/** * Parse an array of timezone objects. A new instance will be created for each object if required. * * @param Array|DateTimeZone|PHPDateTimeZone|string|DateTime|PHPDateTime|null $timezones [optional] An array of the * time zones the specified date and times are in, or null to use the default time zones if the $timezones * param isn't a DateTimeZone instance. A DateTime or PHPDateTime instance to use it's timezone. A single * instance will be converted into an array. The array may be recursive. * @param bool $all [optional] True to require all timezone objects to be parsed successfully, false if not. * @param bool $recursive [optional] True to recursively parse the array, false if not and leave recursive arrays * the way they are. * @param mixed|null $default [optional] The default value returned on failure. This object is also put in the * array as results for objects that couldn't be parsed if $all is false. * * @return Array|mixed An array of DateTimeZone instances or default values, or just the default value on failure. */ public static function parseArray($timezones = null, $all = false, $recursive = true, $default = null) { // Make sure sure the timezone parameter is an arrays if (!is_array($timezones)) { $timezones = array($timezones); } // Parse each timezone foreach ($timezones as $key => &$value) { // Make sure the object isn't an array by itself if (is_array($value)) { // Parse the array if recursive mode is enabled, return the default value on failure if ($recursive) { if (($value = static::parseArray($value, $all, $recursive, $default)) === $default && $all) { return $default; } } // Continue to the next element continue; } // Parse the current object, return the default value if the parsing failed while all objects must be parsed if (($value = DateTimeZoneUtils::parse($value, $default)) === $default && $all) { return $default; } } // Return the result return $timezones; }
/** * Create a DateTime instance from a string with a specified format. * * @param string $format The format used to parse the date time. * @param string $dateTime The date time to parse as a string. * @param DateTimeZone|PHPDateTimeZone|string $timezone The preferred timezone to use, or null to use the default * timezone. * * @return static The DateTime instance or null on failure. * * @throws InvalidDateTimeZoneException Throws InvalidDateTimeZoneException if the timezone is invalid. */ public static function createFromFormat($format, $dateTime, $timezone = null) { // Try to create a DateTime instance based on the input if ($timezone !== null) { // Parse the timezone, throw an exception on failure if (($timezone = DateTimeZoneUtils::parse($timezone, null)) === null) { throw new InvalidDateTimeZoneException('The given timezone is invalid'); } // Create the date and time from the specified format $dateTime = parent::createFromFormat($format, $dateTime, $timezone); } else { $dateTime = parent::createFromFormat($format, $dateTime); } // Make sure the object is valid if (empty($dateTime)) { return null; } // Parse and return the date time return static::parse($dateTime, $timezone); }