w3resource

MySQL UNIX_TIMESTAMP() function

UNIX_TIMESTAMP() function

MySQL UNIX_TIMESTAMP() returns a Unix timestamp in seconds since '1970-01-01 00:00:00' UTC as an unsigned integer if no arguments are passed with UNIX_TIMESTAMP().

When this function used with a date argument, it returns the value of the argument as an unsigned integer in seconds since '1970-01-01 00:00:00' UTC.

The argument may be a DATE, DATETIME,TIMESTAMP or a number in YYYYMMDD or YYMMDD.

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

Syntax:

UNIX_TIMESTAMP(); UNIX_TIMESTAMP(date);

Arguments:

Name Description
date A date value.

Syntax Diagram: 1

MySQL UNIX_TIMESTAMP() Function - Syntax Diagram

Syntax Diagram: 2

MySQL UNIX_TIMESTAMP() Function - Syntax Diagram

MySQL Version: 5.6


Video Presentation:

Pictorial Presentation:

Pictorial Presentation of MySQL UNIX_TIMESTAMP() function

Example:

The following statement will return the unix timestamp in seconds as an unsigned integer since '1970-01-01 00:00:00' UTC.

Code:

SELECT UNIX_TIMESTAMP();

Sample Output:

mysql> SELECT UNIX_TIMESTAMP();
+------------------+
| UNIX_TIMESTAMP() |
+------------------+
|       1429048385 | 
+------------------+
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.0">
<title>example-unix_timestamp-function - php mysql examples | w3resource</title>
<meta name="description" content="example-unix_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>UNIX TIMESTAMP in seconds:</h2>
<table class='table table-bordered'>
<tr>
<th>UNIX TIMESTAMP in seconds</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 UNIX_TIMESTAMP()') as $row) {
echo "<tr>";
echo "<td>" . $row['UNIX_TIMESTAMP()'] . "</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-unix_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 UNIX_TIMESTAMP()";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>UNIX TIMESTAMP in seconds</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("UNIX_TIMESTAMP()")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

Example : UNIX_TIMESTAMP() on a specified date

The following statement will return the unix timestamp in seconds as an unsigned integer since '1970-01-01 00:00:00' UTC for the specified datetime 1970-01-01 12:00:00.

Code:

SELECT UNIX_TIMESTAMP('1970-01-01 12:00:00');

Sample Output:

mysql> SELECT UNIX_TIMESTAMP('1970-01-01 12:00:00');
+---------------------------------------+
| UNIX_TIMESTAMP('1970-01-01 12:00:00') |
+---------------------------------------+
|                                 72000 | 
+---------------------------------------+
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>example1-unix_timestamp-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-unix_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>Unix Timestamp in seconds as an unsigned integer:</h2>
<table class='table table-bordered'>
<tr>
<th>UNIX TIMESTAMP in seconds as an unsigned integer</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 UNIX_TIMESTAMP("1970-01-01 12:00:00")') as $row) {
echo "<tr>";
echo "<td>" . $row['UNIX_TIMESTAMP("1970-01-01 12:00:00")'] . "</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: TO_DAYS()
Next: UTC_DATE()



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