w3resource

MySQL UTC_TIMESTAMP() function

UTC_TIMESTAMP() function

In MySQL, the UTC_TIMESTAMP returns the current UTC date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format depending on the usage of the function i.e. in a string or numeric context.

Note : Since UTC_TIMESTAMP() works on current datetime, your output may vary from the output shown.

Syntax:

UTC_TIMESTAMP; UTC_TIMESTAMP()

Syntax Diagram: 1

MySQL UTC_TIMESTAMP() Function - Syntax Diagram

Syntax Diagram: 2

MySQL UTC_TIMESTAMP() Function - Syntax Diagram

MySQL Version: 5.6


Video Presentation

Pictorial Presentation

Pictorial Presentation of MySQL UTC_TIMESTAMP() function

Example: MySQL UTC_TIMESTAMP() function

The following statement will return the current UTC date and time.

Code:

SELECT UTC_TIMESTAMP,UTC_TIMESTAMP();

Sample Output:

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

Example: UTC_TIMESTAMP() function in numeric format

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

Code:

SELECT UTC_TIMESTAMP()+0;

Sample Output:

mysql> SELECT UTC_TIMESTAMP()+0;
+-----------------------+
| UTC_TIMESTAMP()+0     |
+-----------------------+
| 20150414225635.000000 | 
+-----------------------+
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-UTC_TIMESTAMP-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-UTC_TIMESTAMP-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 time in numeric format:</h2>
<table class='table table-bordered'>
<tr>
<th>Current UTC time 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_TIMESTAMP()+0') as $row) {
echo "<tr>";
echo "<td>" . $row['UTC_TIMESTAMP()+0'] . "</td>";
echo "</tr>"; 
}
?>
</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: UTC_TIME()
Next: WEEK()