将日期转换为此格式

pes*_*sar 7 html php mysql sql datetime

我有这种格式的日期:

   24-12-2010 // DAY - MONTH - YEAR
Run Code Online (Sandbox Code Playgroud)

我需要以这种格式得到它:

   1995-12-31T23:59:59.999Z // The Z is for the TimeZone I think.
Run Code Online (Sandbox Code Playgroud)

检查此链接:

http://lucene.apache.org/solr/api/org/apache/solr/schema/DateField.html

以上链接是我需要日期的方式.

我现在正在使用PHP,所以这需要使用PHP.如何以最简单的方式转换这些日期?

谢谢

Ric*_*son 11

这是ISO8601格式的日期; 以下是你想要的.

gmdate('Y-m-d\TH:i:s\Z', strtotime($date_value));
Run Code Online (Sandbox Code Playgroud)


Chr*_*ian 7

你可以这样做:

$dateTime = new DateTime($myDate);
$formatted = $dateTime->format("Y-m-d\TH:i:s.z\Z");
Run Code Online (Sandbox Code Playgroud)

提到的解决方案:

$dateTime->format(DateTime::W3C);
$dateTime->format(DateTime::ISO8601);
Run Code Online (Sandbox Code Playgroud)

确实返回字符串:

2012-11-28T17:21:11+0100
Run Code Online (Sandbox Code Playgroud)

无法解析,至少使用较新的Solr版本.

如果你需要支持时区,我不会使用gmdate.DateTime实现做得很好,也可用于函数式编程.


Sna*_*ake 2

您可以使用DateTime

$dateTime = new DateTime();
$dateTime.setDate(24, 12, 2010);

$output = $dateTime.format(DateTime::W3C);

// Output now is your date in W3C format.
Run Code Online (Sandbox Code Playgroud)