PHP Date Function and Formatting a Date
March 27, 2008
The other day someone asked me how to format a date pulled from a MySQL dbase. I do this all the time however a friend was asking the question so I decided to blog about it as well. It comes up alot in programming so I figured it might be of use to someone else. Read on.
First of all lets start out with the date you want to format.
2008-03-27 12:00:00
That’s a timestamp, now in order to convert that to lets say, March 03, 2008 12:00 pm, you would need to use two different functions.
The first function would be date() and the secondĀ strtotime().
First you would use strtotime to conver the timestamp to a unix timestamp. So we do something like this.
strtotime(”2008-03-27 12:00:00″)
That basically gives the unix time from the beginning of unix till now by default.
Next we would use the date function and specify what we would like for it to return.
date(’F d, Y h:i a’)
That will give you the full month with day and preceding 0, the full year, 12 hour with leading 0, minutes and am or pm lower case.
Now that the basics are covered what we do in order to convert the timestamp to the time we want is this. Take the strtotime and put it in the date function.
date(’F d, Y h:i a’, strtotime(”2008-03-27 12:00:00″))
This will take the timestamp and convert it to a unix timestamp then the date function will convert that to the date format you specified.
I hope someone finds this useful. Leave a comment if you have questions.
Got something to say?
You must be logged in to post a comment.


