w3resource
MYSQL Tutorial

MySQL GET_FORMAT() function

GET_FORMAT() function

MySQL GET_FORMAT() converts a date or time or datetime in a formatted manner as specified in the argument. This function is useful in combination with DATE_FORMAT(). The various formats have been described bellow.

Syntax:

GET_ FORMAT([date | time | datetime ],[‘EUR’ |‘USA’ |’JIS’|’ISO’|‘INTERNAL’])

Arguments:

Name Description
date | time | datetime A date or time or datetime.
EUR’ | ‘USA’ | ’JIS’ |’ISO’ | ‘INTERNAL Different formats.

Video Presentation

Pictorial Presentation

Pictorial Presentation of MySQL GET_FORMAT() function

Different function calls

Function Call Result
GET_FORMAT(DATE,'USA') '%m.%d.%Y'
GET_FORMAT(DATE,'JIS') '%Y-%m-%d'
GET_FORMAT(DATE,'ISO') '%Y-%m-%d'
GET_FORMAT(DATE,'EUR') '%d.%m.%Y'
GET_FORMAT(DATE,'INTERNAL') '%Y%m%d'
GET_FORMAT(DATETIME,'USA') '%Y-%m-%d %H.%i.%s'
GET_FORMAT(DATETIME,'JIS') '%Y-%m-%d %H:%i:%s'
GET_FORMAT(DATETIME,'ISO') '%Y-%m-%d %H:%i:%s'
GET_FORMAT(DATETIME,'EUR') '%Y-%m-%d %H.%i.%s'
GET_FORMAT(DATETIME,'INTERNAL') '%Y%m%d%H%i%s'
GET_FORMAT(TIME,'USA') '%h:%i:%s %p'
GET_FORMAT(TIME,'JIS') '%H:%i:%s'
GET_FORMAT(TIME,'ISO') '%H:%i:%s'
GET_FORMAT(TIME,'EUR') '%H.%i.%s'
HOUR_MINUTE 'HOURS:MINUTES'
DAY_MICROSECOND 'DAYS HOURS:MINUTES:SECONDS.MICROSECONDS'
DAY_SECOND 'DAYS HOURS:MINUTES:SECONDS'
DAY_MINUTE 'DAYS HOURS:MINUTES'
DAY_HOUR 'DAYS HOURS'
YEAR_MONTH 'YEARS-MONTHS'

Example: MySQL GET_FORMAT() function

The following statement will arrange the date format in EUR.

Code:

SELECT GET_FORMAT(DATE,'EUR');

Sample Output:

mysql> SELECT GET_FORMAT(DATE,'EUR');
+------------------------+
| GET_FORMAT(DATE,'EUR') |
+------------------------+
| %d.%m.%Y               | 
+------------------------+
1 row in set (0.02 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-GET_FORMAT-function - php mysql examples | w3resource</title>
<meta name="description" content="example-GET_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>Format type of a date in EUR format:</h2>
<table class='table table-bordered'>
<tr>
<th>Format type of date in EUR format</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 GET_FORMAT(DATE,"EUR")') as $row) {
echo "<tr>";
echo "<td>" . $row['GET_FORMAT(DATE,"EUR")'] . "</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-get_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 GET_FORMAT(DATE,'EUR')";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Format type of date in EUR format</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("GET_FORMAT(DATE,'EUR')")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

Example: GET_FORMAT() function with DATE_FORMAT()

The following statement will format and return the specified date 2009-05-18 in the format obtained from GET_FORMAT(DATE,'EUR').

Code:

SELECT DATE_FORMAT('2009-05-18',GET_FORMAT(DATE,'EUR'));

Sample Output:

mysql> SELECT DATE_FORMAT('2009-05-18',GET_FORMAT(DATE,'EUR'));
+--------------------------------------------------+
| DATE_FORMAT('2009-05-18',GET_FORMAT(DATE,'EUR')) |
+--------------------------------------------------+
| 18.05.2009                                       | 
+--------------------------------------------------+
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-GET_FORMAT-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-GET_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 specific date 2009-05-18 in EUR format:</h2>
<table class='table table-bordered'>
<tr>
<th>Format type of date in EUR format</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("2009-05-18",GET_FORMAT(DATE,"EUR"))') as $row) {
echo "<tr>";
echo "<td>" . $row['DATE_FORMAT("2009-05-18",GET_FORMAT(DATE,"EUR"))'] . "</td>";
echo "</tr>";  
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Example: GET_FORMAT() function using 'USA' format

The following statement will arrange the time format in USA format.

SELECT GET_FORMAT(TIME,'USA');

Sample Output:

mysql> SELECT GET_FORMAT(TIME,'USA');
+------------------------+
| GET_FORMAT(TIME,'USA') |
+------------------------+
| %h:%i:%s %p            | 
+------------------------+
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>example2-GET_FORMAT-function - php mysql examples | w3resource</title>
<meta name="description" content="example2-GET_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>Format type of time in USA format:</h2>
<table class='table table-bordered'>
<tr>
<th>Format type of time in USA format</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 GET_FORMAT(TIME,"USA")') as $row) {
echo "<tr>";
echo "<td>" . $row['GET_FORMAT(TIME,"USA")'] . "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Example: GET_FORMAT() function with STR_TO_DATE()

The following statement will format and return the specified time 11:15:46 PM in a specific format as obtained from STR_TO_DATE(TIME,'USA').

SELECT STR_TO_DATE('11:15:46 PM',GET_FORMAT(TIME,'USA'));

Sample Output:

mysql> SELECT STR_TO_DATE('11:15:46 PM',GET_FORMAT(TIME,'USA'));
+---------------------------------------------------+
| STR_TO_DATE('11:15:46 PM',GET_FORMAT(TIME,'USA')) |
+---------------------------------------------------+
| 23:15:46                                          | 
+---------------------------------------------------+
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>example3-GET_FORMAT-function - php mysql examples | w3resource</title>
<meta name="description" content="example3-GET_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>Format 11:15:46 PM in USA datetime format:</h2>
<table class='table table-bordered'>
<tr> 
<th>Format 11:15:46 PM in USA datetime format</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 STR_TO_DATE("11:15:46 PM",GET_FORMAT(TIME,"USA"))') as $row) {
echo "<tr>";
echo "<td>" . $row['STR_TO_DATE("11:15:46 PM",GET_FORMAT(TIME,"USA"))'] . "</td>";  
echo "</tr>"; 
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Example : GET_FORMAT() function in 'JIS' format

The following statement will arrange the datetime format in JIS format.

SELECT GET_FORMAT(DATETIME,'JIS');

Sample Output:

mysql> SELECT GET_FORMAT(DATETIME,'JIS');
+----------------------------+
| GET_FORMAT(DATETIME,'JIS') |
+----------------------------+
| %Y-%m-%d %H:%i:%s          | 
+----------------------------+
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>example4-GET_FORMAT-function - php mysql examples | w3resource</title>
<meta name="description" content="example4-GET_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>Format type of datetime in JIS format:</h2>
<table class='table table-bordered'>
<tr>
<th>Format type of datetime in JIS format</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 GET_FORMAT(DATETIME,"JIS")') as $row) { echo "<tr>"; echo "<td>" . $row['GET_FORMAT(DATETIME,"JIS")'] . "</td>"; echo "</tr>"; } ?></tbody> </table> </div> </div> </div> </body> </html>

View the example in browser

Example : GET_FORMAT() function with DATE_FORMAT() and 'JIS' format

The following statement will arrange the datetime 2009-05-18 11:15:46 in a format obtained from GET_FORMAT(DATETIME,'JIS').

Code:

SELECT DATE_FORMAT('2009-05-18 11:15:46',GET_FORMAT(DATETIME,'JIS'));

Sample Output:

mysql> SELECT DATE_FORMAT('2009-05-18 11:15:46',GET_FORMAT(DATETIME,'JIS'));
+---------------------------------------------------------------+
| DATE_FORMAT('2009-05-18 11:15:46',GET_FORMAT(DATETIME,'JIS')) |
+---------------------------------------------------------------+
| 2009-05-18 11:15:46                                           | 
+---------------------------------------------------------------+
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>example5-GET_FORMAT-function - php mysql examples | w3resource</title>
<meta name="description" content="example5-GET_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>Date time of 2009-05-18 11:15:46 in JIS format</h2>
<table class='table table-bordered'>
<tr>
<th>Date time of 2009-05-18 11:15:46 in JIS format</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("2009-05-18 11:15:46",GET_FORMAT(DATETIME,"JIS"))') as $row) {
echo "<tr>";
echo "<td>" . $row['DATE_FORMAT("2009-05-18 11:15:46",GET_FORMAT(DATETIME,"JIS"))'] . "</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: FROM_UNIXTIME()
Next: HOUR()



New Content: Composer: Dependency manager for PHP, R Programming