Skip to content

Instantly share code, notes, and snippets.

@deenison
Last active March 25, 2019 01:31
Show Gist options
  • Save deenison/255b8bac4d0b957ce8f20370ba2112a1 to your computer and use it in GitHub Desktop.
Save deenison/255b8bac4d0b957ce8f20370ba2112a1 to your computer and use it in GitHub Desktop.
Simple conversion of WordPress i18n date format to jQuery UI Datepicker format
<?php
/**
* Converts a given WordPress date format to jQuery UI Datepicker format.
*
* @author Denison Martins <[email protected]>
*
* @see https://codex.wordpress.org/Formatting_Date_and_Time
* @see http://api.jqueryui.com/datepicker
*
* @throws InvalidArgumentException
*
* @param string $date_format_original
*
* @return string
*/
function convert_date_format_to_jqueryui_datepicker($date_format_original)
{
if (!is_string($date_format_original)) {
throw new \InvalidArgumentException('The supplied parameter must be a string.');
}
if (!preg_match_all('/([\w])/', $date_format_original, $current_date_format_terms)) {
return $date_format_original;
}
$format_terms_map = [
'j' => 'd',
'd' => 'dd',
'l' => 'DD',
'n' => 'm',
'm' => 'mm',
'F' => 'MM',
'Y' => 'yy',
'U' => '@',
];
return array_reduce(
array_unique($current_date_format_terms[0]),
function($new_format, $format_term_needle) use ($format_terms_map) {
if (!isset($format_terms_map[$format_term_needle])) {
return $new_format;
}
return str_replace($format_term_needle, $format_terms_map[$format_term_needle], $new_format);
},
$date_format_original
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment