另一个dateTime问题 [英] Another dateTime question
问题描述
我目前有一个采用这种格式的日期
I currently have a date in this format
2010-03-03 10:39:18
这是MySQL中的TIMESTAMP
字段.对于名为Solr的搜索引擎,我需要使用这种格式的日期:
which is a TIMESTAMP
field in MySQL. I need to have the date in this format for a search engine called Solr:
1995-12-31T23:59:59Z
以下是他们网站上有关日期的一些文字:
Here is some text from their website about dates:
Solr预计日期在UTC时 索引.该日期的格式 字段的形式 1995-12-31T23:59:59Z,还有更多 规范形式的限制形式 dateTime的表示形式 http://www.w3.org/TR/xmlschema-2/#dateTime . 尾随的"Z"表示UTC时间 并且是强制性的.可选小数 允许的秒数: 1995-12-31T23:59:59.999Z所有其他 组件是必需的.
Solr expects dates to be in UTC when indexing. The format for this date field is of the form 1995-12-31T23:59:59Z, and is a more restricted form of the canonical representation of dateTime http://www.w3.org/TR/xmlschema-2/#dateTime. The trailing "Z" designates UTC time and is mandatory. Optional fractional seconds are allowed: 1995-12-31T23:59:59.999Z All other components are mandatory.
在SO上的另一个问题中,我得到了这段代码,但这是行不通的. Solr抱怨时间字符串无效":
I was given this code from another Q here on SO, but it doesn't work. Solr complains about an "invalid time string":
$solr_date = date('c', (strtotime($date_from_mysql)); // doesn't work
当回显$solr_date,
时,上面提到的结尾Z
不存在.谢谢.
When echoing $solr_date,
the trailing Z
mentioned above isn't there. Thanks.
推荐答案
为什么不将其转换为UTC?
Why don't you just convert it to UTC?
$datetime = "2010-01-19 00:00:00";
echo "Datetime Before: ".$datetime."<br />";
$dttostr = strtotime("2010-01-19 00:00:00");
echo "Datetime After: ".formatToUTC($dttostr)."<br />";
echo "System Timezone: ".date_default_timezone_get()."<br />";
function formatToUTC($passeddt) {
// Get the default timezone
$default_tz = date_default_timezone_get();
// Set timezone to UTC
date_default_timezone_set("UTC");
// convert datetime into UTC
$utc_format = date("Y-m-d\TG:i:s\Z", $passeddt);
// Might not need to set back to the default but did just in case
date_default_timezone_set($default_tz);
return $utc_format;
}
这篇关于另一个dateTime问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!