/** * 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; }
/** * Check whether the time is local, which is the same time as in the default timezone. * * @return bool True if the time is local, false if not. */ public function isLocal() { return $this->getTimezone()->isLocal(DateTimeZoneUtils::getDefaultTimezone()); }
public static function parse($timezone = null) { // Parse null if ($timezone === null) { return DateTimeZoneUtils::getDefaultTimezone(); } // Parse DateTimeZone instances if ($timezone instanceof self) { return $timezone; } else { if ($timezone instanceof parent) { return new self($timezone); } else { if ($timezone instanceof DateTime) { return $timezone->getTimezone(); } else { if ($timezone instanceof PHPDateTime) { return new self($timezone->getTimezone()); } } } } // If the timezone is a string, make sure the timezone ID is valid, return the default value if not if (is_string($timezone)) { if (DateTimeZoneUtils::isValidTimezoneId($timezone)) { return new self($timezone); } else { return null; } } // Couldn't parse the timezone, return null return null; }