Changing the Format of a Date in PHP

Written by James Mansson on January 18, 2013 Categories: PHP

Sometimes you need to change the format of a date that is in text format. For instance, I recently needed to take a text string containing the date and time of an event, and only display the date part. As the text string was originally generated using the PHP date function, it was easy to do this with a combination of strtotime and date.

The text string was originally generated like this:

$date = date("F j, Y, g:i a");

I stripped out the time part as follows:

$ts = strtotime($date);
$date = date('F j, Y', $ts);

This left me with just the date.

No Comments on Changing the Format of a Date in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *