w3resource

MySQL DATE_FORMAT() function

DATE_FORMAT() function

MySQL DATE_FORMAT() formats a date as specified in the argument. A list of format specifiers given bellow may be used to format a date. The ‘%’ is required before the format specifier characters.

Syntax:

DATE_FORMAT(date,format)

Arguments

Name Description
date A date.
format Indicates how to format a date.

Syntax Diagram:

MySQL DATE_FORMAT() Function - Syntax Diagram

MySQL Version: 5.6


Table of format specifiers

Name Description
%a Abbreviated weekday name (Sun..Sat)
%b Abbreviated month name (Jan..Dec)
%ac Month, numeric (0..12)
%D Day of the month with English suffix (0th, 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 (00..23)
%h Hour (01..12)
%I Hour (01..12)
%i Minutes, numeric (00..59)
%j Day of year (001..366)
%k Hour (0..23)
%l Hour (1..12)
%M Month name (January..December)
%m Month, numeric (00..12)
%p AM or PM
%r Time, 12-hour (hh:mm:ss followed by AM or PM)
%S Seconds (00..59)
%s Seconds (00..59)
%T Time, 24-hour (hh:mm:ss)
%U Week (00..53), where Sunday is the first day of the week
%u Week (00..53), where Monday is the first day of the week
%V Week (01..53), where Sunday is the first day of the week; used with %X
%v Week (01..53), where Monday is the first day of the week; used with %x
%W Weekday name (Sunday..Saturday)
%w Day of the week (0=Sunday..6=Saturday)
%X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%Y Year, numeric, four digits
%y Year, numeric (two digits)
%% A literal “%” character
%x x, for any “x” not listed above

Video Presentation

Pictorial Presentation

Pictorial Presentation of MySQL DATE_FORMAT() function

Example: MySQL DATE_FORMAT() function

select date_format(date, '%a %D %b %Y') 
as formatted_date 
from table_name;

Where date is the name of your date field, and formatted_date is a column alias which you can use as a column heading.

Example Date: 11th February 2011
Replace date with the name of your date field...
date_format String Example
'%Y-%m-%d' 2011-02-11
'%e/%c/%Y' 11/2/2011 UK
'%c/%e/%Y' 2/11/2011 US
'%d/%m/%Y' 11/02/2011 UK
'%m/%d/%Y' 02/11/2011 US
'%e/%c/%Y %H:%i' 11/2/2011 12:30 UK
'%c/%e/%Y %H:%i' 2/11/2011 12:30 US
'%d/%m/%Y %H:%i' 11/02/2011 12:30 UK
'%m/%d/%Y %H:%i' 02/11/2011 12:30 US
'%e/%c/%Y %T' 11/2/2011 12:30:10 UK
'%c/%e/%Y %T' 2/11/2011 12:30:10 US
'%d/%m/%Y %T' 11/02/2011 12:30:10 UK
'%m/%d/%Y %T' 02/11/2011 12:30:10 US
'%a %D %b %Y' Fri 11th Feb 2011
'%a %D %b %Y %H:%i' Fri 11th Feb 2011 12:30
'%a %D %b %Y %T' Fri 11th Feb 2011 12:30:10
'%a %b %e %Y' Fri Feb 11 2011
'%a %b %e %Y %H:%i' Fri Feb 11 2011 12:30
'%a %b %e %Y %T' Fri Feb 11 2011 12:30:10
'%W %D %M %Y' Friday 11th February 2011
'%W %D %M %Y %H:%i' Friday 11th February 2011 12:30
'%W %D %M %Y %T' Friday 11th February 2011 12:30:10
'%l:%i %p %b %e, %Y' 12:30 PM Feb 11, 2011
'%M %e, %Y' February 11, 2011

The following statement will format the specified datetime 2008-05-15 22:23:00 according to the format specifier %W %D %M %Y. Here date has been formatted with week day name, day of the month with english suffix, month name and year in numeric.

Code:

SELECT DATE_FORMAT('2008-05-15 22:23:00', '%W %D %M %Y');

Sample Output:

mysql> SELECT DATE_FORMAT('2008-05-15 22:23:00', '%W %D %M %Y');
+---------------------------------------------------+
| DATE_FORMAT('2008-05-15 22:23:00', '%W %D %M %Y') |
+---------------------------------------------------+
| Thursday 15th May 2008                            | 
+---------------------------------------------------+
1 row in set (0.01 sec)

PHP script

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example-DATE_FORMAT-function - php mysql examples | w3resource</title>
<meta name="description" content="example-DATE_FORMAT-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>Calculating datetime using MySQL:</h2>
<table class='table table-bordered'>
<tr>
<th>Calculated date</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT DATE_FORMAT("2008-05-15 22:23:00", "%W %D %M %Y") as Calculated_Date') as $row) {
echo "<tr>"; 
echo "<td>" . $row['Calculated_Date'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

JSP script

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example-date_format-function</title>
</head>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String Host = "jdbc:mysql://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data = "SELECT DATE_FORMAT('2008-05-15 22:23:00', '%W %D %M %Y') as Calculated_Date";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Calculated date</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("Calculated_Date")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

Example : DATE_FORMAT() function with (%r) specifier

The following statement will format the specified datetime 2008-05-15 22:23:00 according to the format specifier %r. Here the function returns the time in 12-hour format followed by AM or PM.

Code:

SELECT DATE_FORMAT('2008-05-15 22:23:00', '%r');

Sample Output:

mysql> SELECT DATE_FORMAT('2008-05-15 22:23:00', '%r');
+------------------------------------------+
| DATE_FORMAT('2008-05-15 22:23:00', '%r') |
+------------------------------------------+
| 10:23:00 PM                              | 
+------------------------------------------+
1 row in set (0.00 sec)

PHP script

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example1-DATE_FORMAT-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-DATE_FORMAT-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>Calculating datetime using MySQL:</h2>
<table class='table table-bordered'>
<tr>
<th>Calculated date</th>
</tr>
<?php
$hostname="your_hostname"; 
$username="your_username";
$password="your_password";
$db = "your_dbname"; 
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT DATE_FORMAT("2008-05-15 22:23:00", "%r")') as $row) {
echo "<tr>";
echo "<td>" . $row['DATE_FORMAT("2008-05-15 22:23:00", "%r")'] . "</td>"; 
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Example: DATE_FORMAT() function using table

The following statement will format the specified ‘ord_date’ column from purchase table according to the format specifier %W %D %M %Y and display all the rows.

Sample table: purchase


Code:

SELECT invoice_no,ord_date, DATE_FORMAT(ord_date,'%W %D %M %Y')
FROM purchase; 

Sample Output:

 mysql> SELECT invoice_no,ord_date,DATE_FORMAT(ord_date,'%W %D %M %Y')
    ->       FROM purchase;
+------------+------------+-------------------------------------+
| invoice_no | ord_date   | DATE_FORMAT(ord_date,'%W %D %M %Y') |
+------------+------------+-------------------------------------+
| INV0001    | 2008-07-06 | Sunday 6th July 2008                | 
| INV0002    | 2008-08-09 | Saturday 9th August 2008            | 
| INV0003    | 2008-09-15 | Monday 15th September 2008          | 
| INV0004    | 2007-08-22 | Wednesday 22nd August 2007          | 
| INV0005    | 2007-06-25 | Monday 25th June 2007               | 
| INV0006    | 2007-09-20 | Thursday 20th September 2007        | 
+------------+------------+-------------------------------------+
6 rows in set (0.09 sec)
 

PHP script

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example2-DATE_FORMAT-function - php mysql examples | w3resource</title>
<meta name="description" content="example2-DATE_FORMAT-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>A list of invoice no, and order date. Last column shows the order date in weekday name-day of the month-month-year format:</h2>
<table class='table table-bordered'>
<tr>
<th>Invoice no</th><th>Order date</th><th>DATE_FORMAT(ord_date,'%W %D %M %Y')</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT invoice_no,ord_date,
DATE_FORMAT(ord_date,"%W %D %M %Y") 
FROM purchase') as $row) {
echo "<tr>"; 
echo "<td>" . $row['invoice_no'] . "</td>";
echo "<td>" . $row['ord_date'] . "</td>";
echo "<td>" . $row['DATE_FORMAT(ord_date,"%W %D %M %Y")'] . "</td>";
echo "</tr>";  
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Example: DATE_FORMAT() function with where

The following statement will format the specified ‘ord_date’ column from purchase table according to the format specifier %W %D %M %Y and returns those orders which had been placed after 2007.

Sample table: purchase


Code:

SELECT invoice_no,ord_date,            
DATE_FORMAT(ord_date,'%W %D %M %Y')
FROM purchase
WHERE DATE_FORMAT(ord_date,' %Y')>2007;

Sample Output:

 mysql> SELECT invoice_no,ord_date, DATE_FORMAT(ord_date,'%W %D %M %Y')
    ->       FROM purchase
    ->       WHERE DATE_FORMAT(ord_date,' %Y')>2007;
+------------+------------+-------------------------------------+
| invoice_no | ord_date   | DATE_FORMAT(ord_date,'%W %D %M %Y') |
+------------+------------+-------------------------------------+
| INV0001    | 2008-07-06 | Sunday 6th July 2008                | 
| INV0002    | 2008-08-09 | Saturday 9th August 2008            | 
| INV0003    | 2008-09-15 | Monday 15th September 2008          | 
+------------+------------+-------------------------------------+
3 rows in set (0.01 sec)

PHP script

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example3-DATE_FORMAT-function - php mysql examples | w3resource</title>
<meta name="description" content="example3-DATE_FORMAT-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>A list of invoice no, and order date. Last column shows the order date in weekday name-day of the month-month-year format. All the orders listed here are placed after year 2007:</h2>
<table class='table table-bordered'>
<tr>
<th>Invoice no</th><th>Order date</th><th>DATE_FORMAT(ord_date,'%W %D %M %Y')</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT invoice_no,ord_date,
DATE_FORMAT(ord_date,"%W %D %M %Y")
FROM purchase
WHERE DATE_FORMAT(ord_date," %Y")>2007') as $row) {
echo "<tr>";
echo "<td>" . $row['invoice_no'] . "</td>";
echo "<td>" . $row['ord_date'] . "</td>";
echo "<td>" . $row['DATE_FORMAT(ord_date,"%W %D %M %Y")'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

All Date and Time Functions :

Click here to see the MySQL Date and time functions.

Previous: DATE_ADD()
Next: DATE_SUB()



Inviting useful, relevant, well-written and unique guest posts