How to Change the Format of MySQL Dates Using PHP

Dates stored in MySQL databases are represented using the format ‘YYYY-MM-DD’. People don’t usually say they accomplished a task on 2013-01-01, so this format is not the most convenient. Often we need to convert the format of a date stored in a database into a more readable or more user-friendly format.

We can use PHP to re-format dates retrieved from a MySQL database using two functions, DateTime() and date_format().

Note: One requirement for the DateTime() function to work is that the date column in the database must be of the type datetime, not date. Even if the date column started life with the type date, it can be changed at any time. Just realize that dates will now be reported along with the time, which will be all zeroes in this case. To make the date look pretty again, use both functions as outlined below.

A warning is reported when a date column is of the type date when a function expects the type datetime. You might see something like “Warning: date_format() expects parameter 1 to be DateTime, string given” if this happens to you.

Once the dates are recorded as the type datetime, using the date_format() function of PHP will work. Going back to values that are already stored in a database and updating the type is ok, this doesn’t have to happen at the creation of the database. So, if you’re keeping track of dates and want to use a format other than something like “2013-01-01”, keep reading.

As we now know the PHP function date_format() expects the first parameter to be of the type datetime. Here’s how to change date formats:

First, create a new DateTime object. This might not be an obvious thing to do, especially for those of us who use Procedural PHP instead of Object-Oriented PHP.

$gooddate = new DateTime($row['gooddate']);

Then, pass that object ($gooddate) to the PHP function as the first parameter. The second parameter is the format of the date as you’d like it to be seen using date format letters inside a set of single quotes. So, for a complete date and time report, use something like:

$formattedgooddate = date_format($gooddate, 'Y-m-d H:i:s');

The result is a date that looks like “2013-01-01 00:00:00”. Use different date format letters to change how the date appears and don’t use the time letters if you don’t want them to appear on your screen.

$formattedgooddate = date_format($gooddate, 'M j'); will produce ‘Jan 1’ for the date in this case.

Date formatting letters that seem most useful include the following:

  • d – The day of the month (from 01 to 31)
  • D – A short word of a day (three letters)
  • j – The day of the month without leading zeros (1 to 31)
  • l (lowercase ‘L’) – A full word of a day
  • S – Two letter suffix for the day of the month: st, nd, rd or th.
  • z – The day of the year (from 0 through 365)
  • F – A full word of the month (January through December)
  • m – A number of a month, with leading zero (from 01 to 12)
  • M – A short word of a month (three letters)
  • n – A number of a month, no leading zeros (1 to 12)
  • t – The number of days in the given month
  • Y – A four digit year
  • y – A two digit year
  • a – Lowercase am or pm
  • A – Uppercase AM or PM
  • g – 12-hour format of an hour (1 to 12)
  • G – 24-hour format of an hour (0 to 23)
  • i – Minutes with leading zeros (00 to 59)
  • s – Seconds, with leading zeros (00 to 59)
  • e – The timezone identifier (Examples: UTC, GMT, Atlantic/Azores)
  • I (capital i) – If date is in Daylight Savings Rime (1 if yes, 0 otherwise)
  • T – Timezone abbreviations (Examples: EST, MDT)

Check W3Schools for more date formatting characters.

Leave a Reply

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