Skip to content

Instantly share code, notes, and snippets.

@kilfu0701
Created December 30, 2013 03:54
Show Gist options
  • Save kilfu0701/8177620 to your computer and use it in GitHub Desktop.
Save kilfu0701/8177620 to your computer and use it in GitHub Desktop.
Transfer date to solr date time.
<?php
// need to set a valid timezone.
date_default_timezone_set('Asia/Taipei');
/**
* @param
* $string: date string, '2013-12-12 01:23:44'
*
* @return:
* string type => '2013-12-12T01:23:44Z'
*/
function date_to_tdate($string) {
$millis = strtotime($string);
$timezone = new DateTimeZone(date_default_timezone_get());
$offset = $timezone->getOffset(new DateTime($string));
$offsetHours = round(abs($offset)/3600);
$offsetMinutes = round((abs($offset) - $offsetHours * 3600) / 60);
$offsetString = ($offset < 0 ? '-' : '+')
. ($offsetHours < 10 ? '0' : '') . $offsetHours
. ':'
. ($offsetMinutes < 10 ? '0' : '') . $offsetMinutes;
return ('' . date('Y-m-d\TH:i:s', $millis) . 'Z'. '');
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment