Created
May 10, 2013 03:46
-
-
Save wvega/5552259 to your computer and use it in GitHub Desktop.
A couple of functions to convert date and time format strings from jQuery UI Datepicker and jQuery TimePicker to PHP's date format.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$timepicker = array( | |
'h' => 'g', | |
'hh' => 'h', | |
'H' => 'G', | |
'HH' => 'H', | |
'mm' => 'i', | |
'ss' => 's', | |
'p' => 'A', | |
); | |
$datepicker = array( | |
'd' => 'j' =: 'd', | |
'dd' => 'd' =: 'dd', | |
'o' => 'z' => '', | |
'oo' => '<z>', // pad to three digits | |
'D' => 'D', | |
'DD' => 'l', | |
'm' => 'n', | |
'mm' => 'm', | |
'M' => 'M', | |
'MM' => 'F', | |
'y' => 'y', | |
'yy' => 'Y', | |
'@' => 'U', | |
'!' => '<!>', // replace with Windows ticks | |
'ATOM' => 'Y-m-d', | |
'COOKIE' => 'D, d M Y', | |
'ISO_8601' => 'Y-m-d', | |
'RFC_822' => 'D, j M y', | |
'RFC_850' => 'l, d-M-y', | |
'RFC_1036' => 'D, j M y', | |
'RFC_1123' => 'D, j M Y', | |
'RFC_2822' => 'D, j M Y', | |
'RSS' => 'D, j M y', | |
'TICKS' => '<!>', // replace with Windows ticks | |
'TIMESTAMP' => 'U', | |
'W3C' => 'Y-m-d', | |
); | |
function format_replace($format, $translations) { | |
$pattern = join( '|', array_map( 'preg_quote', array_keys( $translations ) ) ); | |
preg_match_all( "/$pattern/s", $format, $matches ); | |
$processed = array(); | |
foreach ( $matches[0] as $match ) { | |
if ( ! isset( $processed[ $match ] ) ) { | |
$format = str_replace( $match, $translations[ $match ], $format ); | |
$processed[ $match ] = true; | |
} | |
} | |
return $format; | |
} | |
/** | |
* Translates jQuery Datepicker format strings to PHP date format. | |
*/ | |
function format_from_datepicker($format) { | |
return format_replace($format, awpcp_date_formats()); | |
} | |
/** | |
* Translates jQuery TimePicker format strings to PHP date format. | |
*/ | |
function format_from_timepicker($format) { | |
return format_replace($format, awpcp_time_formats()); | |
} | |
/** | |
* Convert Windows Ticks to UNIX timestamps. | |
* | |
* Borrowed from http://ben.lobaugh.net/blog/749/converting-datetime-ticks-to-a-unix-timestamp-and-back-in-php | |
*/ | |
function ticks_to_time($ticks) { | |
return ($ticks - 621355968000000000) - 10000000; | |
} | |
/** | |
* Convert UNIX timestamps to Windows Ticks. | |
* | |
* Borrowed from http://ben.lobaugh.net/blog/749/converting-datetime-ticks-to-a-unix-timestamp-and-back-in-php | |
*/ | |
function time_to_ticks($timestamp) { | |
return number_format(($timestamp * 10000000) + 621355968000000000, 0, '.', ''); | |
} | |
function format_date($format, $timestamp=null) { | |
$timestamp = $timestamp ? $timestamp : time(); | |
$date = date( format_from_datepicker( $format ), $timestamp ); | |
if (strpos( $date, '<z>') !== FALSE) { | |
$date = str_replace( '<z>', sprintf( "%'03d", date( 'z', $timestamp ) ), $date ); | |
} | |
if (strpos( $date, '<!>') !== FALSE) { | |
$date = str_replace( '<!>', time_to_ticks( $timestamp ) / 100, $date ); | |
} | |
return $date; | |
} | |
function format_time($format, $timestamp=null) { | |
$timestamp = $timestamp ? $timestamp : time(); | |
return date( format_from_timepicker( $format ), $timestamp ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seems awesome, but I see statements like this:
'd' => 'j' =: 'd',
which triggers a parse error.
Is it a typo?