w3resource

MySQL FROM_UNIXTIME() function

FROM_UNIXTIME() function

MySQL FROM_UNIXTIME() returns a date /datetime from a version of unix_timestamp. The return value is in ‘YYYYY-MM-DD HH:MM:SS’ format or YYYYMMDDHHMMSS.uuuuuu format depending upon the context of the function ( whether numeric or string). If specified, the result is formatted according to a given format string.

Syntax:

FROM_ UNIXTIME (unix_timestamp, [format ])

Arguments

Name Description
unix_timestamp UNIX timestamp.
format A format string indicating the format of the return value.

Syntax Diagram:

MySQL FROM_UNIXTIME() Function - Syntax Diagram

MySQL Version: 5.6


Video Presentation

Pictorial Presentation

Pictorial Presentation of MySQL FROM_UNIXTIME() function

Example: MySQL FROM_UNIXTIME() function

The following statement will return a date time value from 1255033470.

Code:

SELECT FROM_UNIXTIME(1255033470);

Sample Output:

>
mysql> SELECT FROM_UNIXTIME(1255033470);
+---------------------------+
| FROM_UNIXTIME(1255033470) |
+---------------------------+
| 2009-10-08 13:24:30       | 
+---------------------------+
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-FROM_UNIXTIME-function - php mysql examples | w3resource</title>
<meta name="description" content="example-FROM_UNIXTIME-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>Extracting datetime from a given UNIX:</h2>
<table class='table table-bordered'>
<tr>
<th>Extracted datetime</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 FROM_UNIXTIME(1255033470)') as $row) {
echo "<tr>";
echo "<td>" . $row['FROM_UNIXTIME(1255033470)'] . "</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-from_unixtime-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 FROM_UNIXTIME(1255033470)";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Extracted datetime</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("FROM_UNIXTIME(1255033470)")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

Example: FROM_UNIXTIME() function in numeric format

The following statement will return a datetime value in numeric format from 1255033470.

Code:

SELECT FROM_UNIXTIME(1255033470)+0;

Sample Output:

mysql> SELECT FROM_UNIXTIME(1255033470)+0;
+-----------------------------+
| FROM_UNIXTIME(1255033470)+0 |
+-----------------------------+
|       20091008132430.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-FROM_UNIXTIME-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-FROM_UNIXTIME-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>Extracting datetime from a given time:</h2>
<table class='table table-bordered'>
<tr>
<th>Extracted datetime</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 FROM_UNIXTIME(1255033470)+0') as $row) {
echo "<tr>";
echo "<td>" . $row['FROM_UNIXTIME(1255033470)+0'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Example: FROM_UNIXTIME() function with format sperifier

The following statement will return a date time value from 1255033470. The datetime is formatted according to the format string %Y %D %M %h:%i:%s %x.

Code:

SELECT FROM_UNIXTIME(1255033470, '%Y %D %M %h:%i:%s %x');

Sample Output:

mysql> SELECT FROM_UNIXTIME(1255033470, '%Y %D %M %h:%i:%s %x');
+---------------------------------------------------+
| FROM_UNIXTIME(1255033470, '%Y %D %M %h:%i:%s %x') |
+---------------------------------------------------+
| 2009 8th October 01:24:30 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>example2-FROM_UNIXTIME-function - php mysql examples | w3resource</title>
<meta name="description" content="example2-FROM_UNIXTIME-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>Extracting datetime from a given time:</h2>
<table class='table table-bordered'>
<tr>
<th>Extracted datetime</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 FROM_UNIXTIME(1255033470, "%Y %D %M %h:%i:%s %x")') as $row) {
echo "<tr>";
echo "<td>" . $row['FROM_UNIXTIME(1255033470, "%Y %D %M %h:%i:%s %x")'] . "</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_DAYS()
Next: GET_FORMAT()