date_i18n

Retrieve the date in localized format, based on timestamp.

Description

(string) date_i18n( (string) $dateformatstring, (constant) $unixtimestamp = false, (bool) $gmt = false ); 

If the locale specifies the locale month and weekday, then the locale will take over the format for the date. If it isn't, then the date format string will be used instead.

Returns (string)

The date, translated if locale specifies it.

Parameters (3)

0. $dateformatstring (string)
Format to display the date.
1. $unixtimestamp — Optional. (constant) => false
Unix timestamp. Default false.
2. $gmt — Optional. (bool) => false
Whether to use GMT timezone. Default false.

Usage

  1. if ( !function_exists( 'date_i18n' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4. // Format to display the date. 
  5. $dateformatstring = ''; 
  6.  
  7. // Optional. Unix timestamp. Default false. 
  8. $unixtimestamp = false; 
  9.  
  10. // Optional. Whether to use GMT timezone. Default false. 
  11. $gmt = false; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = date_i18n($dateformatstring, $unixtimestamp, $gmt); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/functions.php  
  1. function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) { 
  2. global $wp_locale; 
  3. $i = $unixtimestamp; 
  4.  
  5. if ( false === $i ) { 
  6. $i = current_time( 'timestamp', $gmt ); 
  7.  
  8. /** 
  9. * Store original value for language with untypical grammars. 
  10. * See https://core.trac.wordpress.org/ticket/9396 
  11. */ 
  12. $req_format = $dateformatstring; 
  13.  
  14. if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) { 
  15. $datemonth = $wp_locale->get_month( date( 'm', $i ) ); 
  16. $datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth ); 
  17. $dateweekday = $wp_locale->get_weekday( date( 'w', $i ) ); 
  18. $dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday ); 
  19. $datemeridiem = $wp_locale->get_meridiem( date( 'a', $i ) ); 
  20. $datemeridiem_capital = $wp_locale->get_meridiem( date( 'A', $i ) ); 
  21. $dateformatstring = ' '.$dateformatstring; 
  22. $dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring ); 
  23. $dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring ); 
  24. $dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring ); 
  25. $dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring ); 
  26. $dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring ); 
  27. $dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring ); 
  28.  
  29. $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 ); 
  30. $timezone_formats = array( 'P', 'I', 'O', 'T', 'Z', 'e' ); 
  31. $timezone_formats_re = implode( '|', $timezone_formats ); 
  32. if ( preg_match( "/$timezone_formats_re/", $dateformatstring ) ) { 
  33. $timezone_string = get_option( 'timezone_string' ); 
  34. if ( $timezone_string ) { 
  35. $timezone_object = timezone_open( $timezone_string ); 
  36. $date_object = date_create( null, $timezone_object ); 
  37. foreach ( $timezone_formats as $timezone_format ) { 
  38. if ( false !== strpos( $dateformatstring, $timezone_format ) ) { 
  39. $formatted = date_format( $date_object, $timezone_format ); 
  40. $dateformatstring = ' '.$dateformatstring; 
  41. $dateformatstring = preg_replace( "/([^\\\])$timezone_format/", "\\1" . backslashit( $formatted ), $dateformatstring ); 
  42. $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 ); 
  43. $j = @date( $dateformatstring, $i ); 
  44.  
  45. /** 
  46. * Filters the date formatted based on the locale. 
  47. * @since 2.8.0 
  48. * @param string $j Formatted date string. 
  49. * @param string $req_format Format to display the date. 
  50. * @param int $i Unix timestamp. 
  51. * @param bool $gmt Whether to convert to GMT for time. Default false. 
  52. */ 
  53. $j = apply_filters( 'date_i18n', $j, $req_format, $i, $gmt ); 
  54. return $j;