Joe
Joe

Reputation: 4514

Why is this code failing to parse a date string?

For this MCVE in php

<?
$value = "2016-08-25 (Thu) - 13:36:43";
$date=date_create_from_format("Y-m-d (a) - H:M:S",$value);
echo date_format($date, 'Y-m-d');
?>

I'm getting the error

 Warning: date_format() expects parameter 1 to be DateTimeInterface, boolean given in /home/joereddington/joereddington.com/Jurgen/webclient/temp.php on line 11

It's pretty clear that the date_create_from_format is failing, but I'm struggling to understand why it's failing, or how I can get it to show me an error message?

Upvotes: 0

Views: 43

Answers (1)

Diego de los Reyes
Diego de los Reyes

Reputation: 41

The format is incorrect. The day is D, the minutes are i and the seconds are s. Try this:

$value = "2016-08-25 (Thu) - 13:36:43";
$date=date_create_from_format("Y-m-d (D) - H:i:s",$value);
var_dump($date);
echo date_format($date, 'Y-m-d');

More info:

http://php.net/manual/es/datetime.createfromformat.php

Upvotes: 1

Related Questions