/** * Parses the given string and returns a DateTime object. * * @since Symphony 2.3 * @param string $string (optional) * A string containing date and time, defaults to the current date and time * @return DateTime|boolean * The DateTime object, or if the date could not be parsed, false. */ public static function parse($string) { // Current date and time if ($string == 'now' || empty($string)) { $date = new DateTime(); // Timestamp } elseif (is_numeric($string)) { $date = new DateTime('@' . $string); // Attempt to parse the date provided against the Symphony configuration setting // in an effort to better support multilingual date formats. Should this fail // this block will fallback to just passing the date to DateTime constructor, // which will parse the date assuming it's in an American format. } else { // Standardize date // Convert date string to English $string = Lang::standardizeDate($string); // PHP 5.3: Apply Symphony date format using `createFromFormat` $date = DateTime::createFromFormat(self::$settings['datetime_format'], $string); if ($date === false) { $date = DateTime::createFromFormat(self::$settings['date_format'], $string); } // Handle dates that are in a different format to Symphony's config // DateTime is much the same as `strtotime` and will handle relative // dates. if ($date === false) { try { $date = new DateTime($string); } catch (Exception $ex) { // Invalid date, it can't be parsed return false; } } // If the date is still invalid, just return false. if ($date === false || $date->format('Y') < 0) { return false; } } // Return custom formatted date, use ISO 8601 date by default return $date; }
public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = null) { $status = self::__OK__; $timestamp = null; if (is_null($data) || $data == '') { $timestamp = strtotime(Lang::standardizeDate(DateTimeObj::get(__SYM_DATETIME_FORMAT__, null))); } else { $timestamp = strtotime(Lang::standardizeDate($data)); } if (!is_null($timestamp)) { return array('value' => DateTimeObj::get('c', $timestamp), 'date' => DateTimeObj::getGMT('Y-m-d H:i:s', $timestamp)); } return array('value' => null, 'date' => null); }
/** * 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. * Please note that for best compatibility with European dates it is recommended * that your site be in a PHP5.3 environment. * * @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|boolean * The formatted date, of if the date could not be parsed, false. */ 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('@' . $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(self::$settings['datetime_format'], $string); if ($date === false) { $date = DateTime::createFromFormat(self::$settings['date_format'], $string); } // Handle dates that are in a different format to Symphony's config // DateTime is much the same as `strtotime` and will handle relative // dates. if ($date === false) { try { $date = new DateTime($string); } catch (Exception $ex) { // Invalid date, it can't be parsed return false; } } } else { try { $date = new DateTime($string); } catch (Exception $ex) { // Invalid date, it can't be parsed return false; } } // If the date is still invalid, just return false. if ($date === false) { return false; } } // Timezone // If a timezone was given, apply it if ($timezone !== null) { $date->setTimezone(new DateTimeZone($timezone)); } else { if (isset(self::$settings['timezone'])) { $date->setTimezone(new DateTimeZone(self::$settings['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; }
/** * Parses the given string and returns a DateTime object. * Please note that for best compatibility with European dates it is recommended * that your site be in a PHP5.3 environment. * * @since Symphony 2.3 * @param string $string (optional) * A string containing date and time, defaults to the current date and time * @return DateTime|boolean * The DateTime object, or if the date could not be parsed, false. */ public static function parse($string) { // Current date and time if ($string == 'now' || empty($string)) { $date = new DateTime(); } elseif (is_numeric($string)) { $date = new DateTime('@' . $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(self::$settings['datetime_format'], $string); if ($date === false) { $date = DateTime::createFromFormat(self::$settings['date_format'], $string); } // Handle dates that are in a different format to Symphony's config // DateTime is much the same as `strtotime` and will handle relative // dates. if ($date === false) { try { $date = new DateTime($string); } catch (Exception $ex) { // Invalid date, it can't be parsed return false; } } } else { try { $date = new DateTime($string); } catch (Exception $ex) { // Invalid date, it can't be parsed return false; } } // If the date is still invalid, just return false. if ($date === false) { return false; } } // Return custom formatted date, use ISO 8601 date by default return $date; }
/** * 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; }
/** * Convert string to Datetime object. Log error, if given date is invalid. * * @param string $string * String to be converted to Datetime object * @return Datetime * Returns a Datetime object on success or `NULL` on failure */ private function __getDate($string) { // Get date and time object try { $date = new DateTime(fieldDate::cleanFilterString(Lang::standardizeDate($string))); } catch (Exception $e) { Symphony::$Log->pushToLog('Date and Time could not parse the following date: ' . trim($string) . '. It will be ignored for data source filtering.', E_ERROR, true); $date = NULL; } return $date; }