PHP日期格式无法按预期工作

时间:2014-05-09 14:11:31

标签: php date date-format

给出以下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

1 个答案:

答案 0 :(得分: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);