MySQL DATE_FORMAT(): Format Date and Time Values

MySQL DATE_FORMAT() converts a DATE, DATETIME or TIMESTAMP value into a formatted string. The first argument is the date value and the second is the format pattern.

SELECT DATE_FORMAT(
    '2026-09-09',
    '%d/%m/%Y'
) AS formatted_date;

Output:

09/09/2026

Some common examples:

SELECT DATE_FORMAT(
    '2026-06-23',
    '%d-%M-%Y'
); -- 23-June-2026

SELECT DATE_FORMAT(
    '2026-05-12',
    '%m/%d/%Y %T'
); -- 05/12/2026 00:00:00

SELECT DATE_FORMAT(
    '2026-07-23 22:54:58',
    '%W %D %M %Y %T'
);
DATE_FORMAT() changes the display value, not the stored date. The result is text, so keep the original DATE/DATETIME column for sorting, filtering and date calculations.

MySQL DATE_FORMAT() Syntax Top ↑

DATE_FORMAT(date_value, format_string)

The format string contains specifiers beginning with %. For example, %Y gives a four-digit year, %m gives a two-digit month and %d gives a two-digit day.

Format a Date Column from a MySQL Table Top ↑

If dt_tb contains a DATETIME column named dt, format it directly in the SELECT list:

SELECT id,
       dt,
       DATE_FORMAT(
           dt,
           '%m/%d/%Y %T'
       ) AS my_date
FROM dt_tb;

The original database value remains unchanged. Only the returned my_date column is formatted.

Common DATE_FORMAT() Examples Top ↑

FormatExample output
%W %D %M %Y %TWednesday 5th May 2004 23:56:25
%a %b %e %Y %H:%iWed May 5 2004 23:56
%m/%d/%Y %T05/05/2004 23:56:25
%d/%m/%Y05/05/2004
%Y-%m-%d2004-05-05
%r11:56:25 PM

For machine-readable database work, keep dates stored in native MySQL date types. Use DATE_FORMAT() mainly when you need a specific text representation in query output.

DATE_FORMAT() Format Specifiers Top ↑

SpecifierDescription
%aAbbreviated weekday name (Sun..Sat)
%bAbbreviated month name (Jan..Dec)
%cMonth, numeric (0..12)
%DDay of the month with English suffix (1st, 2nd, 3rd, ...)
%dDay of the month, numeric (00..31)
%eDay of the month, numeric (0..31)
%fMicroseconds (000000..999999)
%HHour, 24-hour format (00..23)
%hHour, 12-hour format (01..12)
%IHour, 12-hour format (01..12)
%iMinutes (00..59)
%jDay of year (001..366)
%kHour, 24-hour format (0..23)
%lHour, 12-hour format (1..12)
%MMonth name (January..December)
%mMonth, numeric (00..12)
%pAM or PM
%r12-hour time (hh:mm:ss AM/PM)
%SSeconds (00..59)
%sSeconds (00..59)
%T24-hour time (hh:mm:ss)
%UWeek (00..53), Sunday first; WEEK() mode 0
%uWeek (00..53), Monday first; WEEK() mode 1
%VWeek (01..53), Sunday first; used with %X
%vWeek (01..53), Monday first; used with %x
%WWeekday name (Sunday..Saturday)
%wDay of week (0=Sunday..6=Saturday)
%XWeek-based year for Sunday-first weeks; used with %V
%xWeek-based year for Monday-first weeks; used with %v
%YFour-digit year
%yTwo-digit year
%%Literal percent character
Do not confuse %m and %i. In MySQL DATE_FORMAT(), %m is the month and %i is minutes.

Filtering by Month or Year Top ↑

DATE_FORMAT() can be used in a WHERE condition:

SELECT id, dt
FROM dt_tb
WHERE DATE_FORMAT(
    dt,
    '%Y-%m'
) = '2004-05';

However, when you are filtering a real DATE or DATETIME column, comparing the original column to a date range is often a better pattern:

SELECT id, dt
FROM dt_tb
WHERE dt >= '2004-05-01 00:00:00'
  AND dt <  '2004-06-01 00:00:00';

This avoids wrapping the table column in a formatting function and gives MySQL a better opportunity to use an index on dt.

Use DATE_FORMAT() for presentation. For date-range filtering, compare the stored date/datetime value directly whenever practical.

Sorting Formatted Dates Correctly Top ↑

If a formatted date is returned as text, sorting that display string can produce an order different from chronological order.

Prefer:

SELECT id,
       dt,
       DATE_FORMAT(
           dt,
           '%d/%m/%Y'
       ) AS display_date
FROM dt_tb
ORDER BY dt ASC;

The original DATETIME column controls chronology while DATE_FORMAT() controls only the displayed text.

DATE_FORMAT() and NULL Values Top ↑

If the date expression is NULL, DATE_FORMAT() returns NULL:

SELECT DATE_FORMAT(
    NULL,
    '%d/%m/%Y'
);

If the application needs display text for missing dates, handle that separately:

SELECT COALESCE(
    DATE_FORMAT(
        dt,
        '%d/%m/%Y'
    ),
    'Not available'
) AS display_date
FROM dt_tb;

Month and Weekday Names Top ↑

Formats such as %M, %W, %a and %b return language-dependent month or weekday names. MySQL uses the session's lc_time_names setting for these names.

SELECT @@lc_time_names;

For an English-language tutorial, examples such as June and Sunday assume an English time-name locale.

MySQL DATE_FORMAT() vs PHP Date Formatting Top ↑

There are two valid places to format a database date for display:

  • MySQL: use DATE_FORMAT() when the query itself should return formatted text.
  • PHP: return the native date value and format it in application code when presentation belongs in the PHP layer.

The format symbols are not the same between MySQL and PHP, so do not copy a format pattern from one language directly into the other.

Formatting a MySQL Date in PHP

Modern Sample Table for DATE_FORMAT() Top ↑

The original version of this tutorial used zero dates such as '0000-00-00' and the old TYPE=MyISAM syntax. Those examples are no longer a good default for modern MySQL.

A cleaner sample table is:

CREATE TABLE dt_tb (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    dt DATETIME NULL DEFAULT NULL,
    dt2 DATE NULL DEFAULT NULL,
    PRIMARY KEY (id)
) ENGINE=InnoDB
  DEFAULT CHARSET=utf8mb4;

INSERT INTO dt_tb
    (dt, dt2)
VALUES
    (
        '2004-10-26 00:00:00',
        '2005-01-25'
    ),
    (
        '2004-05-05 23:56:25',
        '2005-06-12'
    ),
    (
        '2005-12-08 13:20:10',
        '2005-06-06'
    );

Use SQL NULL when a date is genuinely unknown instead of inventing an invalid zero-date placeholder.

Common DATE_FORMAT() Mistakes Top ↑

Using DATE_FORMAT() to change the stored value Top ↑

DATE_FORMAT() returns formatted text. It does not rewrite the underlying DATE or DATETIME value.

Filtering an indexed date column only through DATE_FORMAT() Top ↑

For month or date-range searches, direct boundary comparisons are often more index-friendly than applying DATE_FORMAT() to every row.

Sorting by a display string instead of the date column Top ↑

Formats such as dd/mm/yyyy are text. Sort by the native date column when chronological order matters.

Confusing MySQL and PHP format symbols Top ↑

The formatting tokens differ. For example, MySQL uses %i for minutes.

Using zero dates as a missing-value placeholder Top ↑

Modern MySQL configurations can reject invalid zero dates. Use a valid date or NULL according to the data model.

Assuming month and weekday names are always English Top ↑

Named months and weekdays depend on MySQL's lc_time_names setting.

Frequently Asked Questions Top ↑

Q1: What does DATE_FORMAT() do in MySQL?

DATE_FORMAT() converts a DATE, DATETIME or TIMESTAMP expression into a formatted text string using a format pattern.

Q2: Does DATE_FORMAT() change the value stored in the table?

No. It changes only the value returned by the query.

Q3: What is the MySQL format for day/month/year?

Use %d/%m/%Y, for example DATE_FORMAT(dt, '%d/%m/%Y').

Q4: What is the difference between %m and %i?

%m represents the numeric month, while %i represents minutes.

Q5: Should DATE_FORMAT() be used in WHERE for month filtering?

It can be used, but direct date-range comparisons are often preferable because they preserve the native date comparison and can make index use easier.

Q6: What happens when DATE_FORMAT() receives NULL?

It returns NULL.

Q7: Should an unknown date be stored as 0000-00-00?

No. Use a valid date or SQL NULL according to the column's meaning and constraints.



SQL Date References MONTH() DAY() NOW(): Current Date with Time


Subscribe to our YouTube Channel here



plus2net.com
gurpreet grewal

29-03-2010

this is very useful website ....... to read it i solve my prob.... thanx to plus to net.
kasun

16-09-2010

hi, i want to know about how to read blob file in mysql database..but i know how to insert photo in to the data base. plz help me....




SQL Video Tutorials










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer