Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Determines the correct date format for the current WordPress locale.
- *
- * This function makes it extremely easy to keep PHP date formatting and
- * jQuery datepicker identical. It retrieves the correct date format based
- * on offical ICU formatting, bypassing the need for the PHP Intl module.
- *
- * @uses wp-locale-to-date-format.json
- * @see https://pastebin.com/eT3ku5fR
- *
- * @param string $length short|full
- * @param string $script Optional. Either php or jquery. Defaults to php.
- * @return string
- */
- function stonehenge_localize_date_format( $length, $script = 'php' ) {
- $locale = str_replace( '_formal', '', determine_locale() ); // Dates are neither formal nor informal.
- $json = file_get_contents( __DIR__ . '/assets/wp-locale-to-date-format.json' );
- $source = json_decode( $json, true );
- return array_key_exists( $locale, $source ) ? $source[ $locale ][ $script ][ $length ] : ( 'php' === $script ? 'Y-m-d' : 'yy-mm-dd' );
- }
- /**
- * Returns the localized date by the given formatting type.
- *
- * Prevents errors by checking if the supplied input is numeric.
- *
- * Example:
- * `echo stonehenge_localize_date( $value );` => 27-08-2024 (d-m-Y)
- * `echo stonehenge_localize_date( $value, 'full' );` => dinsdag 27 augustus 2024 (l j F Y)
- *
- * @param string $date Any (un)formatted date string.
- * @param string $length Optional. The short of full date format. Defaults to short.
- * @return string
- */
- function stonehenge_localize_date( $date, $length = 'short' ) {
- $date = is_numeric( $date ) ? $date : strtotime( $date ); // Prevent errors.
- $format = stonehenge_localize_date_format( 'php', $length );
- return date_i18n( $format, $date );
- }
- /**
- * Returns localized values for the jQuery UI datepicker using the WP_Locale globals.
- *
- * This function removes the need for any third-party translation files and prevents incorrect
- * date format processing by the function `wp_localize_jquery_ui_datepicker`.
- *
- * @global WP_Locale $wp_locale WordPress date and time locale object.
- * @return array
- */
- function stonehenge_localize_datepicker() {
- global $weekday, $weekday_abbrev, $month, $month_abbrev;
- // Correct the WP_Locale globals as most languages only use lowercase mid sentence.
- return array(
- 'date_format' => esc_attr( stonehenge_localize_date_format( 'short', 'jquery' ) ),
- 'days_full' => array_map( 'ucfirst', $weekday ),
- 'days_short' => array_map( 'ucfirst', $weekday_abbrev ),
- 'months_full' => array_map( 'ucfirst', $month ),
- 'months_short' => array_map( 'ucfirst', $month_abbrev ),
- 'first_day' => absint( get_option( 'start_of_week' ) ),
- 'next' => esc_attr__( 'Next' ),
- 'previous' => esc_attr__( 'Previous' ),
- 'close' => esc_attr__( 'Close' ),
- 'today' => esc_attr__( 'Today' )
- );
- }
- /**
- * Use wp_localize_script() to send the localized jQuery date picker options.
- */
- wp_localize_script( 'your-script-handle', 'object', stonehenge_localize_datepicker() );
- /**
- * Call the localized values in your script.js
- */
- jQuery(document).ready( function($) {
- $.datepicker({
- dateFormat: object.date_format, // dd-mm-yy
- dayNames: object.days_full, // Zondag, Maandag, Dinsdag, Woensdag ...
- dayNamesShort: object.days_short, // Zo, Ma, Di, Wo ...
- monthNames: object.months_full, // Januari, Februari, Maart, April ...
- monthNamesShort: object.months_short, // Jan, Feb, Maa, Apr ...
- firstDay: object.first_day, // 1 (Monday)
- prevText: object.previous, // Vorige
- nextText: object.next, // Volgende
- currentText: object.today, // Vandaag
- yearRange: "-1:+4", // 2023 - 2028
- changeMonth: true,
- changeYear: true
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment