2011-12-28 62 views
6

我在字符串函数新的,所以我需要一个复杂的SUBSTR和装饰功能,该字符串:字符串DateTime对象

Wed, 28 Dec 2011 13:04:30 GMT 

字符串来我总是与这种格式。我想将它转换为DateTime对象。任何人都可以帮助我?

+0

你尝试过这个'$时间=的strtotime($日期);' – Dotnet 2011-12-28 13:22:39

+0

为什么你就不能使用日期时间:: createfromformat() - http://php.net/manual/en/datetime.createfromformat.php? – 2011-12-28 13:22:50

+0

strtotime函数不接受这种格式。 datetime :: createfromformat也许可以解决这种情况。谢谢你的帮助,我现在要去看看。 – MAB 2011-12-28 13:25:58

回答

13
$dateString = 'Wed, 28 Dec 2011 13:04:30 GMT'; 
$dateTime = datetime::createfromformat('D, d M Y H:i:s e',$dateString); 

echo $dateTime->format('d-M-Y H:i:s e'); 
+0

非常感谢你,我想我可以用你的解决方案解决这个问题。我要检查并再次转向。 – MAB 2011-12-28 13:33:26

+1

DateTime的构造函数将接受这个dateString而不需要定义格式。 – Armin 2011-12-28 14:02:19

10
<?php 
$date = new DateTime('Wed, 28 Dec 2011 13:04:30 GMT'); 
echo $date->format('r'); 

...打印:

Wed, 28 Dec 2011 13:04:30 +0000 
相关问题