当前位置: 首页>>代码示例>>PHP>>正文


PHP IntlDateFormatter::getErrorMessage方法代码示例

本文整理汇总了PHP中IntlDateFormatter::getErrorMessage方法的典型用法代码示例。如果您正苦于以下问题:PHP IntlDateFormatter::getErrorMessage方法的具体用法?PHP IntlDateFormatter::getErrorMessage怎么用?PHP IntlDateFormatter::getErrorMessage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IntlDateFormatter的用法示例。


在下文中一共展示了IntlDateFormatter::getErrorMessage方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: parse_date

 /**
  * parses a date string into UNIX timestamp
  *
  * if "strict dates" is set, this function uses the DateTime or IntlDateFormatter 
  * class to parse the string according to a specific format. If it is not, we 
  * use the conventional strtotime() function, with the enhancement that if the 
  * non-American style format is used with slashes "d/m/Y" the string is prepared 
  * so strtotime can parse it correctly  
  *
  * @param string $string      the string to parse; if not given, defaults to now
  * @param object $column_atts the column object; used to identify the field for
  *                            user feedback
  * @param bool   $zero_time   if set, zero the time portion of the date so it 
  *                            won't interfere with date comparisons
  * @return int|bool UNIX timestamp or false if parse fails
  */
 public static function parse_date($string = false, $column = '', $zero_time = false)
 {
     if (false === $string) {
         return false;
     }
     $string = Participants_Db::set_filter('parse_date', $string, $column);
     // is it already a timestamp?
     if (self::is_valid_timestamp($string)) {
         //if (WP_DEBUG and is_object($column)) error_log(__METHOD__.' tried to parse timestamp from '. $column->name);
         return $string;
     }
     $date = false;
     // if it is a default zero timestamp, treat it as "no date"
     if ($string === '0000-00-00 00:00:00') {
         return false;
     }
     /*
      * we have two options to parse a date string into a timestamp: the 
      * IntlDateFormatter class or the DateTime class. The IntlDateFormatter 
      * class can parse localized text dates, but it seems commonly unavailable, 
      * at least on English-speaking servers. The DateTime class is widely 
      * available, but can't parse non-English text dates. It can parse numeric 
      * date representations, so if the intl module is not available, we try to 
      * use DateTime. If that is not available, we use strtotime with the added trick 
      * of swapping the date/month if they are slashes so slashed European notation 
      * can be correctly parsed
      */
     self::$date_mode = 'none';
     $errors = false;
     $the_Date = false;
     // test for MySQL-format timestamp
     $is_MySQL_timestamp = preg_match('#^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$#', $string) === 1 ? true : false;
     //error_log(__METHOD__.' object? '.(is_object($column)?'yes':'no').' strict dates? '.(self::plugin_setting_is_true('strict_dates', false)?'yes':'no').' timestamp? '.($is_MySQL_timestamp?'yes':'no'));
     if (self::plugin_setting_is_true('strict_dates', false) and is_object($column) and !$is_MySQL_timestamp) {
         //error_log(__METHOD__.' intl? '.(class_exists('IntlDateFormatter')?'yes':'no').' datetime? '.(class_exists('DateTime')?'yes':'no'));
         if (class_exists('IntlDateFormatter')) {
             self::$date_mode = 'Intl';
             $DateFormat = new IntlDateFormatter(get_locale(), IntlDateFormatter::LONG, IntlDateFormatter::NONE, NULL, NULL, Participants_Db::get_ICU_date_format(self::$plugin_options['input_date_format']));
             $DateFormat->setLenient(false);
             // we want it strict
             $timestamp = $DateFormat->parse($string);
             if ($DateFormat->getErrorCode() !== 0) {
                 $errors = array('code' => $DateFormat->getErrorCode(), 'error' => $DateFormat->getErrorMessage());
             }
             if (!$errors) {
                 $the_Date = new DateTime();
                 $the_Date->setTimestamp($timestamp);
             } elseif (WP_DEBUG) {
                 error_log(__METHOD__ . ' IntlDateFormatter error: format string: ' . Participants_Db::get_ICU_date_format(self::$plugin_options['input_date_format']) . ' timestamp: ' . $timestamp . ' formatter error: ' . $DateFormat->getErrorMessage());
             }
         }
         if (!$the_Date && class_exists('DateTime')) {
             self::$date_mode = 'DateTime';
             $the_Date = DateTime::createFromFormat(self::$plugin_options['input_date_format'], $string);
         }
         if (is_object($the_Date)) {
             $errors = $the_Date->getLastErrors();
             if ($errors['warning_count'] === 0 && $errors['error_count'] === 0) {
                 $errors = false;
             }
         }
         if (is_array($errors) && !empty($string)) {
             $the_Date = false;
             if (is_object(self::$validation_errors) and is_object($column)) {
                 self::$validation_errors->add_error($column->name, sprintf(__('The date for "%s" was invalid. Please input the date with the exact format shown', 'participants-database'), $column->title));
             }
             if (WP_DEBUG) {
                 error_log(__METHOD__ . ' DateTime parse error: ' . implode(', ', $errors));
             }
         }
         /*
          * if we have a valid date, convert to timestamp
          */
         if ($the_Date) {
             /*
              * zero the time so date equality comparisons can be made
              */
             if ($zero_time) {
                 $the_Date->setTime(0, 0);
             }
             $date = $the_Date->format('U');
         }
     }
     /*
//.........这里部分代码省略.........
开发者ID:raj-rk,项目名称:Raj,代码行数:101,代码来源:participants-database.php

示例2: getFormattedDateTime

 /**
  * Formats a date/time value.
  * If the value is null also null will be returned.
  * @param mixed $value Date/time value to be formatted using {@link http://php.net/manual/intldateformatter.format.php}.
  * @param string $locale Locale to be used with {@link http://php.net/manual/class.intldateformatter.php}.
  * @param integer $datetype Date format. See {@link http://php.net/manual/class.intldateformatter.php#intl.intldateformatter-constants} for valid values.
  * @param integer $timetype Time format. See {@link http://php.net/manual/class.intldateformatter.php#intl.intldateformatter-constants} for valid values.
  * @return string Formatted date/time.
  * @throws \InvalidArgumentException
  */
 protected function getFormattedDateTime($value, $locale, $datetype, $timetype)
 {
     if ($value === null) {
         return null;
     }
     $valueToUse = $value;
     // IntlDateFormatter#format() doesn't support \DateTime objects prior to PHP 5.3.4 (http://php.net/manual/intldateformatter.format.php)
     if ($valueToUse instanceof \DateTime) {
         // \DateTime::getTimestamp() would return false for year > 2038 on 32-bit systems (https://bugs.php.net/bug.php?id=50590)
         $valueToUse = floatval($valueToUse->format('U'));
     } elseif (is_string($valueToUse)) {
         $valueToUse = floatval($valueToUse);
     }
     $localeToUse = !empty($locale) ? $locale : $this->getLocale();
     $formatter = new \IntlDateFormatter($localeToUse, $datetype, $timetype, date_default_timezone_get());
     $result = $formatter->format($valueToUse);
     if ($result === false) {
         throw new \InvalidArgumentException(sprintf('The value "%s" of type %s cannot be formatted. Error: "%s".', $value, gettype($value), $formatter->getErrorMessage()));
     }
     return $result;
 }
开发者ID:rrehbeindoi,项目名称:TwigExtensionsBundle,代码行数:31,代码来源:FormatDateTimeExtension.php


注:本文中的IntlDateFormatter::getErrorMessage方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。