2014-05-09 28 views
0

考虑下面的PHP代码:PHP日期格式无法正常运行

$thestring = "Saturday - 05/10/2014 at 10:00 am"; 

echo $thestring."\n"; 

$thestring = str_replace("at ", "", $thestring); 

echo $thestring."\n"; 

$thestring = substr($thestring, strpos($thestring, " - ")+3); 

echo $thestring."\n"; 

$thedate = date_create_from_format("m/d/Y h:m a", $thestring); 

echo $thedate->format("Ymd")."\n"; 
echo $thedate->format("Y")."\n"; 
echo $thedate->format("m")."\n"; 
echo $thedate->format("d")."\n"; 

为什么我会得到下面的输出?月份和年份关闭了,我没有看到它的合理解释。

Saturday - 05/10/2014 at 10:00 am 
Saturday - 05/10/2014 10:00 am 
05/10/2014 10:00 am 
20131210 
2013 
12 
10 

回答

3

m是几个月的修饰符。 i是分钟的修饰符。解决这个问题显然会造成问题。更改:

$thedate = date_create_from_format("m/d/Y h:m a", $thestring); 

到:

$thedate = date_create_from_format("m/d/Y h:i a", $thestring); 
+0

我亲眼看见的。 PEBKAC。 –

+0

@MattLachman由于大多数错误都会;) –