Example #1
0
 /**
  * Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string.
  * It takes into account the default date format for the current language if a LC_TIME file is used.
  *
  * @see \Cake\Utility\Time::i18nFormat()
  *
  * @param int|string|\DateTime $date UNIX timestamp, strtotime() valid string or DateTime object
  * @param string $format strftime format string.
  * @param bool|string $invalid Default value to display on invalid dates
  * @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
  * @return string Formatted and translated date string
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  * @throws \InvalidArgumentException When the date cannot be parsed
  */
 public function i18nFormat($date, $format = null, $invalid = false, $timezone = null)
 {
     try {
         $time = new Time($date, $timezone);
         return $time->i18nFormat($format, $timezone);
     } catch (\Exception $e) {
         if ($invalid === false) {
             throw $e;
         }
         return $invalid;
     }
 }
Example #2
0
 /**
  * test formatting dates taking in account preferred i18n locale file
  *
  * @return void
  */
 public function testI18nFormat()
 {
     $time = new Time('Thu Jan 14 13:59:28 2010');
     $result = $time->i18nFormat();
     $expected = '1/14/10, 1:59 PM';
     $this->assertTimeFormat($expected, $result);
     $result = $time->i18nFormat(\IntlDateFormatter::FULL, null, 'es-ES');
     $expected = 'jueves, 14 de enero de 2010, 13:59:28 (GMT)';
     $this->assertTimeFormat($expected, $result);
     $format = [\IntlDateFormatter::NONE, \IntlDateFormatter::SHORT];
     $result = $time->i18nFormat($format);
     $expected = '1:59 PM';
     $this->assertTimeFormat($expected, $result);
     $result = $time->i18nFormat('HH:mm:ss', 'Australia/Sydney');
     $expected = '00:59:28';
     $this->assertTimeFormat($expected, $result);
     Time::$defaultLocale = 'fr-FR';
     $result = $time->i18nFormat(\IntlDateFormatter::FULL);
     $expected = 'jeudi 14 janvier 2010 13:59:28 UTC';
     $this->assertTimeFormat($expected, $result);
     $result = $time->i18nFormat(\IntlDateFormatter::FULL, null, 'es-ES');
     $expected = 'jueves, 14 de enero de 2010, 13:59:28 (GMT)';
     $this->assertTimeFormat($expected, $result, 'DEfault locale should not be used');
 }