Web20 University

Working with Date Formats in PHP and MySQL: A Complete Guide

Last edited on
Get up to 65% Off Hosting with FREE SSL & FREE Domains!

* Web 2.0 University is supported by it's audience. If you purchase through links on our site, we may earn an affiliate commision.

When working with databases and web applications, date and time handling is crucial. PHP and MySQL provide robust support for date manipulation, but understanding how to select, format, and manage dates properly can save you time and avoid confusion down the line. In this post, we’ll explore how to work with date formats in PHP and MySQL, including how to format dates in select statements.

Table of Contents

  1. Introduction to Date Formats in PHP and MySQL
  2. Default Date Format in MySQL
  3. Formatting Dates in PHP
  4. Formatting Dates in MySQL with SELECT Statements
  5. Examples of Using DATE_FORMAT() in MySQL
  6. Conclusion

1. Introduction to Date Formats in PHP and MySQL

In both PHP and MySQL, dates are often stored in a standard format such as YYYY-MM-DD. However, developers frequently need to display dates in more user-friendly formats like October 3, 2024 or 03-10-2024. PHP and MySQL provide powerful functions to convert these default formats into whatever format you need.

In MySQL, dates are stored in formats like DATE, DATETIME, TIMESTAMP, etc., while PHP offers the DateTime object and the date() function to handle date manipulations.


2. Default Date Format in MySQL

MySQL stores dates in a specific format, which depends on the data type used:

  • DATE: YYYY-MM-DD (e.g., 2024-10-03)
  • DATETIME: YYYY-MM-DD HH:MM:SS (e.g., 2024-10-03 15:30:00)
  • TIMESTAMP: Same as DATETIME but used for timestamps with time zones.

These formats are suitable for storage but may not be readable or user-friendly when displayed on a website. That’s where date formatting functions come in.


3. Formatting Dates in PHP

In PHP, the date() function is commonly used to format dates. Here’s an example of how you can format the current date:

echo date("Y-m-d"); // Outputs the date in YYYY-MM-DD format

You can customize this function to format the date in any way you want:

echo date("F j, Y"); // Outputs: October 3, 2024

To format dates fetched from MySQL, you can use the DateTime object in PHP:

$date = new DateTime('2024-10-03');
echo $date->format('d-m-Y'); // Outputs: 03-10-2024

4. Formatting Dates in MySQL with SELECT Statements

MySQL’s DATE_FORMAT() function allows you to format the date directly within the SQL query, making it easy to fetch data from the database in the desired format. This can be particularly useful for reports or user-facing applications where you need to present data in a readable way.

Syntax of DATE_FORMAT():

SELECT DATE_FORMAT(date_column, 'format') FROM table;

In this syntax:

  • date_column: The column containing the date.
  • 'format': The format in which you want the date to appear.

Some common format specifiers include:

  • %Y: Year in 4 digits (e.g., 2024)
  • %m: Month in 2 digits (e.g., 10 for October)
  • %d: Day in 2 digits (e.g., 03 for the 3rd day)
  • %W: Full weekday name (e.g., Thursday)
  • %M: Full month name (e.g., October)

5. Examples of Using DATE_FORMAT() in MySQL

Let’s take a look at some practical examples of how to use DATE_FORMAT() in your SQL queries.

Example 1: Displaying the Date in DD-MM-YYYY Format

If your orders table has a column order_date with the default MySQL date format (YYYY-MM-DD), you can format it as DD-MM-YYYY using:

SELECT order_id, DATE_FORMAT(order_date, '%d-%m-%Y') AS formatted_date FROM orders;

Output:

order_idformatted_date
103-10-2024
201-10-2024

Example 2: Displaying Full Date with Month and Year

To display the date in a more human-readable format, like October 3, 2024, you can use:

SELECT order_id, DATE_FORMAT(order_date, '%M %d, %Y') AS formatted_date FROM orders;

Output:

order_idformatted_date
1October 3, 2024
2October 1, 2024

Example 3: Displaying the Day of the Week

To display the day of the week for each order:

SELECT order_id, DATE_FORMAT(order_date, '%W') AS day_of_week FROM orders;

Output:

order_idday_of_week
1Thursday
2Tuesday

Example 4: Combining Time and Date

If you’re working with a DATETIME or TIMESTAMP field and want to format both date and time, you can use:

SELECT order_id, DATE_FORMAT(order_date, '%M %d, %Y at %h:%i %p') AS formatted_date_time FROM orders;

Output:

order_idformatted_date_time
1October 3, 2024 at 03:30 PM
2October 1, 2024 at 12:00 PM

6. Conclusion

Handling dates in PHP and MySQL can be simple and efficient if you know the right functions to use. MySQL’s DATE_FORMAT() and PHP’s date() functions provide the flexibility to format dates in a way that suits your application and user needs. By mastering these tools, you can ensure that your applications display dates in a readable, user-friendly manner.

Whether you’re building reports, generating logs, or simply displaying dates on your web pages, knowing how to select and format dates properly will save you time and help avoid potential pitfalls.

Feel free to explore more about the formatting options in PHP’s official documentation and MySQL’s DATE_FORMAT() reference.


Get up to 65% Off Hosting with FREE SSL & FREE Domains!