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(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.
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.
| Format | Example output |
|---|---|
%W %D %M %Y %T | Wednesday 5th May 2004 23:56:25 |
%a %b %e %Y %H:%i | Wed May 5 2004 23:56 |
%m/%d/%Y %T | 05/05/2004 23:56:25 |
%d/%m/%Y | 05/05/2004 |
%Y-%m-%d | 2004-05-05 |
%r | 11: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.
| Specifier | Description |
|---|---|
%a | Abbreviated weekday name (Sun..Sat) |
%b | Abbreviated month name (Jan..Dec) |
%c | Month, numeric (0..12) |
%D | Day of the month with English suffix (1st, 2nd, 3rd, ...) |
%d | Day of the month, numeric (00..31) |
%e | Day of the month, numeric (0..31) |
%f | Microseconds (000000..999999) |
%H | Hour, 24-hour format (00..23) |
%h | Hour, 12-hour format (01..12) |
%I | Hour, 12-hour format (01..12) |
%i | Minutes (00..59) |
%j | Day of year (001..366) |
%k | Hour, 24-hour format (0..23) |
%l | Hour, 12-hour format (1..12) |
%M | Month name (January..December) |
%m | Month, numeric (00..12) |
%p | AM or PM |
%r | 12-hour time (hh:mm:ss AM/PM) |
%S | Seconds (00..59) |
%s | Seconds (00..59) |
%T | 24-hour time (hh:mm:ss) |
%U | Week (00..53), Sunday first; WEEK() mode 0 |
%u | Week (00..53), Monday first; WEEK() mode 1 |
%V | Week (01..53), Sunday first; used with %X |
%v | Week (01..53), Monday first; used with %x |
%W | Weekday name (Sunday..Saturday) |
%w | Day of week (0=Sunday..6=Saturday) |
%X | Week-based year for Sunday-first weeks; used with %V |
%x | Week-based year for Monday-first weeks; used with %v |
%Y | Four-digit year |
%y | Two-digit year |
%% | Literal percent character |
%m and %i. In MySQL DATE_FORMAT(), %m is the month and %i is minutes.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.
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.
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;
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.
There are two valid places to format a database date for display:
DATE_FORMAT() when the query itself should return formatted text.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 PHPThe 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.
DATE_FORMAT() returns formatted text. It does not rewrite the underlying DATE or DATETIME value.
For month or date-range searches, direct boundary comparisons are often more index-friendly than applying DATE_FORMAT() to every row.
Formats such as dd/mm/yyyy are text. Sort by the native date column when chronological order matters.
The formatting tokens differ. For example, MySQL uses %i for minutes.
Modern MySQL configurations can reject invalid zero dates. Use a valid date or NULL according to the data model.
Named months and weekdays depend on MySQL's lc_time_names setting.
DATE_FORMAT() converts a DATE, DATETIME or TIMESTAMP expression into a formatted text string using a format pattern.
No. It changes only the value returned by the query.
Use %d/%m/%Y, for example DATE_FORMAT(dt, '%d/%m/%Y').
%m represents the numeric month, while %i represents minutes.
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.
It returns NULL.
No. Use a valid date or SQL NULL according to the column's meaning and constraints.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| 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.... | |