public DateFormatter::format($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL)
Formats a date, using a date type or a custom date format string.
Parameters
int $timestamp: A UNIX timestamp to format.
string $type: (optional) The format to use, one of:
- One of the built-in formats: 'short', 'medium', 'long', 'html_datetime', 'html_date', 'html_time', 'html_yearless_date', 'html_week', 'html_month', 'html_year'.
- The name of a date type defined by a date format config entity.
- The machine name of an administrator-defined date format.
- 'custom', to use $format.
Defaults to 'medium'.
string $format: (optional) If $type is 'custom', a PHP date format string suitable for input to date(). Use a backslash to escape ordinary text, so it does not get interpreted as date format characters.
string|null $timezone: (optional) Time zone identifier, as described at http://php.net/manual/timezones.php Defaults to the time zone used to display the page.
string|null $langcode: (optional) Language code to translate to. NULL (default) means to use the user interface language for the page.
Return value
string A translated date string in the requested format. Since the format may contain user input, this value should be escaped when output.
Overrides DateFormatterInterface::format
File
- core/lib/Drupal/Core/Datetime/DateFormatter.php, line 103
Class
- DateFormatter
- Provides a service to handle various date related functionality.
Namespace
Drupal\Core\Datetime
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | public function format( $timestamp , $type = 'medium' , $format = '' , $timezone = NULL, $langcode = NULL) { if (!isset( $timezone )) { $timezone = date_default_timezone_get(); } // Store DateTimeZone objects in an array rather than repeatedly // constructing identical objects over the life of a request. if (!isset( $this ->timezones[ $timezone ])) { $this ->timezones[ $timezone ] = timezone_open( $timezone ); } if ( empty ( $langcode )) { $langcode = $this ->languageManager->getCurrentLanguage()->getId(); } // Create a DrupalDateTime object from the timestamp and timezone. $create_settings = array ( 'langcode' => $langcode , 'country' => $this ->country(), ); $date = DrupalDateTime::createFromTimestamp( $timestamp , $this ->timezones[ $timezone ], $create_settings ); // If we have a non-custom date format use the provided date format pattern. if ( $date_format = $this ->dateFormat( $type , $langcode )) { $format = $date_format ->getPattern(); } // Fall back to medium if a format was not found. if ( empty ( $format )) { $format = $this ->dateFormat( 'fallback' , $langcode )->getPattern(); } // Call $date->format(). $settings = array ( 'langcode' => $langcode , ); return $date ->format( $format , $settings ); } |
Please login to continue.