Вопрос

Given the following PHP code:

$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";

Why am I getting the output below? The month and year are off and I don't see a logical explanation for it.

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
Это было полезно?

Решение

m is the modifier for months. i is the modifier for minutes. Getting this wrong will obviously cause issues. Change:

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

to:

$thedate = date_create_from_format("m/d/Y h:i a", $thestring);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top