StonehengeCreations

WP Locale to PHP & jQuery date format

Jan 26th, 2021 (edited)
529
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * Get the correct localized date formatting by WordPress Locale.
  3. *
  4. * Retrieves the short or full date formatting for PHP and jQuery (Date Picker) by WordPress Locale.
  5. * (WordPress Dashboard -> Settings -> General)
  6. *
  7. * Formats are based on the official ICU formatting. This resource bypasses the need for the Intl module.
  8. * The JSON file 'wp-locale-to-date-format.json' is located here: https://pastebin.com/eT3ku5fR
  9. */
  10. function stonehenge_localize_date_format( $type = 'php', $length = 'short' ) {
  11.     $json       = file_get_contents( __DIR__ . '/wp-locale-to-date-format.json' );
  12.     $locales    = json_decode( $json, true );
  13.     $locale     = str_replace( '_formal', '', determine_locale() );     // Dates are not different in formal.
  14.     return array_key_exists( $locale, $locales ) ? $locales[$locale][$type][$length] : ($type != 'jquery' ? 'Y-m-d' : 'yy-mm-dd');
  15. }
  16.  
  17. /**
  18. * Localize the date for PHP output.
  19. * Usage:
  20. * echo stonehenge_localize_date( $value );              => 26-01-2021 (d-m-Y)
  21. * echo stonehenge_localize_date( $value, 'full' );      => dinsdag 26 januari 2021 (l j F Y)
  22. */
  23. function stonehenge_localize_date( $date, $length = 'short' ) {
  24.     $date   = is_numeric( $date ) ? $date : strtotime( $date );     // Prevent errors.
  25.     $format = stonehenge_localize_date_format( 'php', $length );
  26.     return date_i18n( $format, $input );
  27. }
  28.  
  29. /**
  30. * Localize jQuery Date Picker by WordPress Locale.
  31. */
  32. function stonehenge_localize_datepicker( $type ) {
  33.     global $wp_locale;
  34.     switch( $type ) {
  35.         case 'months_full':
  36.             $values = array_values( $wp_locale->month );
  37.         break;
  38.         case 'months_short':
  39.             $values = array_values( $wp_locale->month_abbrev );
  40.         break;
  41.         case 'weekdays_full':
  42.             $values = array_values( $wp_locale->weekday );
  43.         break;
  44.         case 'weekdays_short':
  45.             $values = array_values( $wp_locale->weekday_abbrev );
  46.         break;
  47.     }
  48.     return array_map( 'ucfirst', $values );     // Correction, as most languages only use lowercase mid-sentence.
  49. }
  50.  
  51. /**
  52. * Use wp_localize_script() to send the localized jQuery date picker options.
  53. */
  54. wp_localize_script( 'your-script-handle', 'your-object-name', array(
  55.     'dateFormat' => stonehenge_localize_date_format( 'jquery', 'short' ),
  56.     'monthNames' => stonehenge_localize_datepicker( 'months_full' ),
  57.     'monthNamesShort' => stonehenge_localize_datepicker( 'months_short' ),
  58.     'dayNames' => stonehenge_localize_datepicker( 'weekdays_full' ),
  59.     'dayNamesShort' => stonehenge_localize_datepicker( 'weekdays_short' ),
  60.     'firstDay' => get_option( 'start_of_week' ),
  61.     'prevText' => __('Previous'),
  62.     'nextText' => __('Next'),
  63.     'currentText' => __('Today')
  64. ) );
  65.  
  66. /**
  67. * Call the localized values in your script.js
  68. */
  69. jQuery(document).ready( function($) {
  70.     $.datepicker({
  71.         dateFormat: object.dateFormat,              // dd-mm-yy
  72.         monthNames: object.monthNames,              // Januari, Februari, Maart, April ...
  73.         monthNamesShort: object.monthNamesShort,    // Jan, Feb, Maa, Apr ...
  74.         dayNames: object.dayNames,                  // Zondag, Maandag, Dinsdag, Woensdag ...
  75.         dayNamesShort: object.dayNamesShort,        // Zo, Ma, Di, Wo ...
  76.         firstDay: object.firstDay,                  // 1 (Monday)
  77.         prevText: object.prevText,                  // Vorige
  78.         nextText: object.nextText,                  // Volgende
  79.         currentText: object.currentText,            // Vandaag
  80.         yearRange: "-1:+4",                         // 2020 - 2025
  81.         changeMonth: true,
  82.         changeYear: true
  83.     });
  84. });
RAW Paste Data