Created
December 30, 2013 03:54
-
-
Save kilfu0701/8177620 to your computer and use it in GitHub Desktop.
Transfer date to solr date time.
This file contains 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 | |
// 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