The DateFormat class allows you to format dates and times with predefined styles in a locale-sensitive manner. Formatting times with the DateFormat class is similar to formatting dates. Formatting dates with the DateFormat class is a two-step process. First, you create a formatter with the getDateInstance method. Second, you invoke the format method, which returns a string containing the formatted date. DateTime values are formatted using standard or custom patterns stored in the properties of a DateTimeFormatInfo.
Example #1
0
 public function testCustomPatterns()
 {
     $dateFormatter = new DateFormat();
     $time = @mktime(9, 9, 9, 9, 1, 2004);
     $pattern = "'Hello' EEEE, 'it should be' MMM yyyy HH:mm:ss!!!";
     $wants = 'Hello Wednesday, it should be Sep 2004 09:09:09!!!';
     $this->assertEquals($wants, $dateFormatter->format($time, $pattern));
 }
Example #2
0
 /**
  * Renders the localized version of the date-time value.
  * If the culture is not specified, the default application
  * culture will be used.
  * This method overrides parent's implementation.
  */
 protected function getFormattedDate()
 {
     $value = $this->getValue();
     $defaultText = $this->getDefaultText();
     if (empty($value) && !empty($defaultText)) {
         return $this->getDefaultText();
     }
     $app = $this->getApplication()->getGlobalization();
     //initialized the default class wide formatter
     if (self::$formatter === null) {
         self::$formatter = new DateFormat($app->getCulture());
     }
     $culture = $this->getCulture();
     //return the specific cultural formatted date time
     if (strlen($culture) && $app->getCulture() !== $culture) {
         $formatter = new DateFormat($culture);
         return $formatter->format($value, $this->getPattern(), $this->getCharset());
     }
     //return the application wide culture formatted date time.
     $result = self::$formatter->format($value, $this->getPattern(), $this->getCharset());
     return $result;
 }