2010-03-01 78 views

回答

2

可以使用DateTime

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

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

// Output now is your date in W3C format. 
+0

Unfort这不符合Solr的工作。输出将是“2012-11-28T17:21:11 + 0100”,这是错误的。 – Christian 2014-07-12 13:19:16

+1

另外。 notation在PHP中不存在。 – Christian 2014-07-12 13:21:06

9

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

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

我不会这样建议,因为它很容易出错。更好地提供自己的输入格式,并使用DateTime :: W3C作为输出格式。 – Snake 2010-03-01 09:32:42

+0

如果输入格式是固定且可预测的,则它是可靠的(例如来自DB)。如果输入格式来自用户,则始终需要对其进行重新分析。 – 2010-03-01 09:43:45

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

时间戳你可以做这样的事情:

$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实现做得很好,也可用于函数式编程。