将日期转换为此格式

时间:2010-03-01 09:19:41

标签: php sql mysql html datetime

我有这种格式的日期:

   24-12-2010 // DAY - MONTH - YEAR

我需要以这种格式获取它:

   1995-12-31T23:59:59.999Z // The Z is for the TimeZone I think.

检查此链接:

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

以上链接是我需要约会的方式。

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

由于

5 个答案:

答案 0 :(得分:10)

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

gmdate('Y-m-d\TH:i:s\Z', strtotime($date_value));

答案 1 :(得分:6)

你可以这样做:

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

上述解决方案:

$dateTime->format(DateTime::W3C);
$dateTime->format(DateTime::ISO8601);

会返回如下字符串:

2012-11-28T17:21:11+0100

无法解析,至少在较新的Solr版本中。

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

答案 2 :(得分:2)

您可以使用DateTime

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

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

// Output now is your date in W3C format.

答案 3 :(得分:0)

使用php的日期(字符串$ format [,int $ timestamp])函数! 在第二个参数中使用http://php.net/manual/en/function.strtotime.php从字符串中获取时间戳

答案 4 :(得分:0)

$date = strtotime('24-12-2010');
$new_date = gmDate("Y-m-d\TH:i:s.z\Z",$date);