Question

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
Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top