2014-05-09 4 views
0

Vu le code PHP suivant:PHP Dateformat ne fonctionne pas comme prévu

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

Pourquoi suis-je recevoir la sortie ci-dessous? Le mois et l'année sont éteints et je ne vois pas d'explication logique à cela.

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 

Répondre

3

m est le modificateur pour les mois. i est le modificateur pour les minutes. Se tromper va évidemment causer des problèmes. Changer:

$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

Je l'ai juste vu moi-même. PEBKAC. –

+0

@MattLachman Comme la plupart des erreurs font;) –

Questions connexes