w3resource

MySQL UTC_DATE() function

UTC_DATE() function

MySQL UTC_DATE returns the current UTC (Coordinated Universal Time) date as a value in 'YYYY-MM-DD' or YYYYMMDD format depending on the context of the function i.e. in a string or numeric context.

Syntax:

UTC_DATE; UTC_DATE()

Syntax Diagram: 1

MySQL UTC_DATE() Function - Syntax Diagram

Syntax Diagram: 2

MySQL UTC_DATE() Function - Syntax Diagram

MySQL Version: 5.6


Video Presentation:

Pictorial Presentation:

Pictorial Presentation of MySQL UTC_DATE() function

Example:

The following statement will return the current UTC date.

Code:

SELECT UTC_DATE,UTC_DATE();

Sample Output:

mysql> SELECT UTC_DATE,UTC_DATE();
+------------+------------+
| UTC_DATE   | UTC_DATE() |
+------------+------------+
| 2015-04-14 | 2015-04-14 | 
+------------+------------+
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.0">
<title>example-UTC_DATE-function - php mysql examples | w3resource</title>
<meta name="description" content="example-UTC_DATE-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>Current UTC date in two different ways:</h2>
<table class='table table-bordered'>
<tr>
<th>Current UTC Date using UTC_DATE</th><th>Current UTC Date using UTC_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 UTC_DATE,UTC_DATE()') as $row) {
echo "<tr>";
echo "<td>" . $row['UTC_DATE'] . "</td>";
echo "<td>" . $row['UTC_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-utc_date-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 UTC_DATE,UTC_DATE()";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Current UTC Date using UTC_DATE</td>
<td>Current UTC Date using UTC_DATE()</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("UTC_DATE")%></TD>
<TD><%=rs.getString("UTC_DATE()")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

Example : UTC_DATE() function in numeric format

The following statement will return the current UTC date in numeric format.

Code:

SELECT UTC_DATE()+0;

Sample Output:

mysql> SELECT UTC_DATE()+0;
+--------------+
| UTC_DATE()+0 |
+--------------+
|     20150414 | 
+--------------+
1 row in set (0.04 sec)

PHP script:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>example1-UTC_DATE-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-UTC_DATE-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>Current UTC date in numeric format:</h2>
<table class='table table-bordered'>
<tr>
<th>Current UTC Date in numeric 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 UTC_DATE()+0') as $row) {
echo "<tr>";
echo "<td>" . $row['UTC_DATE()+0'] . "</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: UNIX_TIMESTAMP()
Next: UTC_TIME()



Follow us on Facebook and Twitter for latest update.




We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook