Format date in PHP

Given the string “2013-12-29”, what PHP code snippet will produce “December 29, 2013”?


$d = new \\DateTime("2013-12-29");
echo $d->format('F d, Y');

:slight_smile:

I have to admit. That’s a good one. Works great.

I know this is a solved issue but, I will make it complete.
This will work for PHP >= 5.2.0

A “classical” option is

<?php
echo date("F d, Y", strtotime('2013-12-29'));
?>

Still, the OOP version is better, if PHP >= 5.2.

Good suggestion. But better on not, I’d still go for the classical version even in newer php versions because it’s faster :). Of course, only when this simple conversion is all I want to do.

Of course most times you’d run the two statements of the OOP version at completely different times - the first when the data is retrieved from the database and the second at whichever spot you wanted to output it. They would usually be inside methods belonging to different objects.

The classic option is only really appropriate to procedural coding.