Use PHP to format date from MySQL
I needed to display a date correctly on a web page that was retrieved from a MySQL database.
Although the date was inserted into the database with a British format of dd/mm/yyyy, it is getting stored as a timestamp within the SQL.
So when retrieving the date later it would show as
mm/dd/yy. This was in an American format and not what I required.
What I needed to do was retrieve the timestamp from the database and use php to convert / format the date accordingly for my purpose.
Using a combination of the date() and strtotime() functions as follows I was able to format the date to my requirements:
The date() function can be used to change the format with various recognised characters in the parameter string. For my needs the popular ones were:
d = Day of the month (2 digits with leading zeros 01 to 31)
D = Day of the month (three letters Mon through Sun)
m = Month (leading zeros 01 through 12)
M = Month (three letters Jan through Dec)
Y = Year (4 digits 1999 or 2020)
y = Year (2 digits 99, 01 or 20)