/** * Formats the given date and time `$string` based on the given `$format`. * Optionally the result will be localized and respect a timezone differing * from the system default. The default output is ISO 8601. * * @since Symphony 2.2.1 * @param string $string (optional) * A string containing date and time, defaults to the current date and time * @param string $format (optional) * A valid PHP date format, defaults to ISO 8601 * @param boolean $localize (optional) * Localizes the output, if true, defaults to true * @param string $timezone (optional) * The timezone associated with the timestamp * @return string * The formatted date */ public static function format($string = 'now', $format = DateTime::ISO8601, $localize = true, $timezone = null) { // Current date and time if ($string == 'now' || empty($string)) { $date = new DateTime(); } elseif (is_numeric($string)) { $date = new Datetime(date(DateTime::ISO8601, $string)); } else { // Standardize date // Convert date string to English $string = Lang::standardizeDate($string); // PHP 5.3: Apply Symphony date format using `createFromFormat` if (method_exists('DateTime', 'createFromFormat')) { $date = DateTime::createFromFormat(__SYM_DATETIME_FORMAT__, $string); if ($date === false) { $date = DateTime::createFromFormat(__SYM_DATE_FORMAT__, $string); } } else { $date = strptime($string, DateTimeObj::dateFormatToStrftime(__SYM_DATETIME_FORMAT__)); if ($date === false) { $date = DateTimeObj::dateFormatToStrftime(__SYM_DATE_FORMAT__, $string); } if (is_array($date)) { $date = date(DateTime::ISO8601, mktime($date['tm_hour'], $date['tm_min'], $date['tm_sec'], $date['tm_mon'] + 1, $date['tm_mday'], 1900 + $date['tm_year'])); $date = new DateTime($date); } } // Handle non-standard dates (ie. relative dates, tomorrow etc.) if ($date === false) { $date = new DateTime($string); } } // Timezone if ($timezone !== null) { $date->setTimezone(new DateTimeZone($timezone)); } // Format date $date = $date->format($format); // Localize date // Convert date string from English back to the activated Language if ($localize === true) { $date = Lang::localizeDate($date); } // Return custom formatted date, use ISO 8601 date by default return $date; }