StonehengeCreations

WordPress locale to PHP & jQuery date formats

Jan 26th, 2021 (edited)
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.68 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Determines the correct date format for the current WordPress locale.
  4.  *
  5.  * This function makes it extremely easy to keep PHP date formatting and
  6.  * jQuery datepicker identical. It retrieves the correct date format based
  7.  * on offical ICU formatting, bypassing the need for the PHP Intl module.
  8.  *
  9.  * @uses   wp-locale-to-date-format.json
  10.  * @see    https://pastebin.com/eT3ku5fR
  11.  *
  12.  * @param  string $length short|full
  13.  * @param  string $script Optional. Either php or jquery. Defaults to php.
  14.  * @return string
  15.  */
  16. function stonehenge_localize_date_format( $length, $script = 'php' ) {
  17.     $locale = str_replace( '_formal', '', determine_locale() ); // Dates are neither formal nor informal.
  18.     $json   = file_get_contents( __DIR__ . '/assets/wp-locale-to-date-format.json' );
  19.     $source = json_decode( $json, true );
  20.  
  21.     return array_key_exists( $locale, $source ) ? $source[ $locale ][ $script ][ $length ] : ( 'php' === $script ? 'Y-m-d' : 'yy-mm-dd' );
  22. }
  23.  
  24. /**
  25.  * Returns the localized date by the given formatting type.
  26.  *
  27.  * Prevents errors by checking if the supplied input is numeric.
  28.  *
  29.  * Example:
  30.  * `echo stonehenge_localize_date( $value );`           => 27-08-2024 (d-m-Y)
  31.  * `echo stonehenge_localize_date( $value, 'full' );`   => dinsdag 27 augustus 2024 (l j F Y)
  32.  *
  33.  * @param  string $date   Any (un)formatted date string.
  34.  * @param  string $length Optional. The short of full date format. Defaults to short.
  35.  * @return string
  36.  */
  37. function stonehenge_localize_date( $date, $length = 'short' ) {
  38.     $date   = is_numeric( $date ) ? $date : strtotime( $date );  // Prevent errors.
  39.     $format = stonehenge_localize_date_format( 'php', $length );
  40.  
  41.     return date_i18n( $format, $date );
  42. }
  43.  
  44. /**
  45.  * Returns localized values for the jQuery UI datepicker using the WP_Locale globals.
  46.  *
  47.  * This function removes the need for any third-party translation files and prevents incorrect
  48.  * date format processing by the function `wp_localize_jquery_ui_datepicker`.
  49.  *
  50.  * @global WP_Locale $wp_locale WordPress date and time locale object.
  51.  * @return array
  52.  */
  53. function stonehenge_localize_datepicker() {
  54.     global $weekday, $weekday_abbrev, $month, $month_abbrev;
  55.  
  56.     // Correct the WP_Locale globals as most languages only use lowercase mid sentence.
  57.     return array(
  58.         'date_format'   => esc_attr( stonehenge_localize_date_format( 'short', 'jquery' ) ),
  59.         'days_full'     => array_map( 'ucfirst', $weekday ),
  60.         'days_short'    => array_map( 'ucfirst', $weekday_abbrev ),
  61.         'months_full'   => array_map( 'ucfirst', $month ),
  62.         'months_short'  => array_map( 'ucfirst', $month_abbrev ),
  63.         'first_day'     => absint( get_option( 'start_of_week' ) ),
  64.         'next'          => esc_attr__( 'Next' ),
  65.         'previous'      => esc_attr__( 'Previous' ),
  66.         'close'         => esc_attr__( 'Close' ),
  67.         'today'         => esc_attr__( 'Today' )
  68.     );
  69. }
  70.  
  71. /**
  72.  * Use wp_localize_script() to send the localized jQuery date picker options.
  73.  */
  74. wp_localize_script( 'your-script-handle', 'object', stonehenge_localize_datepicker() );
  75.  
  76. /**
  77.  * Call the localized values in your script.js
  78.  */
  79. jQuery(document).ready( function($) {
  80.     $.datepicker({
  81.         dateFormat: object.date_format,         // dd-mm-yy
  82.         dayNames: object.days_full,             // Zondag, Maandag, Dinsdag, Woensdag ...
  83.         dayNamesShort: object.days_short,       // Zo, Ma, Di, Wo ...
  84.         monthNames: object.months_full,         // Januari, Februari, Maart, April ...
  85.         monthNamesShort: object.months_short,   // Jan, Feb, Maa, Apr ...
  86.         firstDay: object.first_day,             // 1 (Monday)
  87.         prevText: object.previous,              // Vorige
  88.         nextText: object.next,                  // Volgende
  89.         currentText: object.today,              // Vandaag
  90.         yearRange: "-1:+4",                     // 2023 - 2028
  91.         changeMonth: true,
  92.         changeYear: true
  93.     });
  94. });
Advertisement
Add Comment
Please, Sign In to add comment