/** * Parse an array of date and time objects with optional time zones. A new instance will be created for each object * if required. * * If the $dateTimes parameter is a DateTime zone instance, the instance will be returned and the $timezone * parameter is ignored. If the $dateTime parameter is anything other than a DateTime zone the date, time and the * time zone is parsed through the constructor. * * @param Array|DateTime|string|null $dateTimes [optional] An array of DateTime or PHPDateTime instance, the time * as a string, or null to use the now() time. A single instance will be converted into an array. The array may * be recursive. * @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 $time param * isn't a DateTime instance. A DateTime or PHPDateTime instance to use it's timezone. The number of timezones * must be equal to the number of date and time objects. If only one timezone is given the timezone will be * used for all date and time objects. The array may be recursive. * @param bool $recursive [optional] True to recursively parse the array, false if not and leave recursive arrays * the way they are. * @param bool $flat [optional] True to force the returned array to be flat, false to keep the same levels of the given array. * @param bool $all [optional] True to require all date time objects to be parsed successfully, false if not. * @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 DateTime instances or default values, or just the default value on failure. */ public static function parseArray($dateTimes = null, $timezones = null, $recursive = true, $flat = false, $all = false, $default = null) { // Make sure sure the date and time and the timezone parameters are arrays if (!is_array($dateTimes)) { $dateTimes = array($dateTimes); } // Parse the timezones array, return the default value on failure if (($timezones = DateTimeZoneArrayUtils::parseArray($timezones, $all, $recursive, null)) === null) { return $default; } // Make sure the number of timezones is equal to one or the number of date and time objects $dateTimesCount = count($dateTimes); $timezonesCount = count($timezones); if ($timezonesCount != 1 && $timezonesCount != $dateTimesCount) { return $default; } // Parse each date and time foreach ($dateTimes as $key => &$value) { // Get the timezone for the current object $timezone = $timezonesCount > 1 ? $timezones[$key] : $timezones[0]; // 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) { // Make the array flat if required if ($flat) { array_splice($dateTimes, $key, 1, $value); } // Parse the value if (($value = static::parseArray($value, $timezone, $recursive, $flat, $all, $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 = DateTimeUtils::parse($value, $timezone, $default)) === $default && $all) { return $default; } } // Return the result return $dateTimes; }
/** * Check whether a timezone is local relative to this timezone. This compares the offset of both timezones at a * specific point in time. * * @param DateTimeZone|PHPDateTimeZone|string|DateTime|PHPDateTime|null $timezone [optional] A DateTimeZone or * PHPDateTimeZone instance, the timezone ID as a string, a DateTime or PHPDateTime instance to use it's * timezone or null to use the defualt timezone. * @param DateTime|PHPDateTime|string|null $dateTime [optional] The specific point in date and time to compare the * offsets at. A DateTime or PHPDateTime instance, the date and time as a string or null to use the now() * value. * * @return bool True if the timezone is local, false if not. False will also be returned on failure. */ public function isLocal($timezone = null, $dateTime = null) { // Parse the timezone, return false on failure if (($timezone = static::parse($timezone)) === null) { return false; } // Parse the date and time, return false on failure if (($dateTime = DateTimeUtils::parse($dateTime, $timezone, null)) === null) { return false; } // Compare the offsets of both timezones at the specific point in time, return the result return $this->getOffset($dateTime) == $timezone->getOffset($dateTime); }