w3resource

MySQL LOCALTIMESTAMP() Function

LOCALTIMESTAMP() Function

MySQL LOCALTIMESTAMP returns the value of current date and time in ‘YYYY-MM-DD HH:MM:SS’ format or YYYMMDDHHMMSS.uuuuuu format depending on the context (numeric or string) of the function.

CURRENT_TIMESTAMP, CURRENT_TIMESTAMP(), NOW, NOW(), LOCALTIME, LOALTIME(), LOCALTIMESTAMP() are the synonyms of LOCALTIMESTAMP.

The LOCALTIMESTAMP returns the constant time when the statement began to work.

Note: Output of the examples of this page will depend on the current time.

Syntax:

LOCALTIMESTAMP

Syntax Diagram:

MySQL LOCALTIMESTAMP() Function - Syntax Diagram

MySQL Version: 5.6


Video Presentation:

Pictorial Presentation:

Pictorial Presentation of MySQL LOCALTIMESTAMP() function

Example: MySQL LOCALTIMESTAMP

The following statement will return the current date and time in ‘YYYY-MM-DD HH:SS:MM’ format.

Code:

SELECT LOCALTIMESTAMP;

Sample Output:

mysql> SELECT LOCALTIMESTAMP;
+---------------------+
| LOCALTIMESTAMP      |
+---------------------+
| 2015-04-13 17:15:16 | 
+---------------------+
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-localtimestamp-function - php mysql examples | w3resource</title>
<meta name="description" content="example-localtimestamp-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>Displaying current date and time in YYYY-MM-DD HH:SS:MM format:</h2>
<table class='table table-bordered'>
<tr>
<th>Current date and time in YYYY-MM-DD HH:SS:MM 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 LOCALTIMESTAMP') as $row) {
echo "<tr>";
echo "<td>" . $row['LOCALTIMESTAMP'] . "</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-localtimestamp-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 LOCALTIMESTAMP";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Current date and time in YYYY-MM-DD HH:SS:MM format</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("LOCALTIMESTAMP")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

Example: LOCALTIMESTAMP in numeric format

The following statement will return the current date and time in ‘YYYY-MM-DD HH:SS:MM’ format as well as YYYYMMDDHHSSMM.uuuuuu format.

Code:

SELECT LOCALTIMESTAMP,LOCALTIMESTAMP+1;

Sample Output:

mysql> SELECT LOCALTIMESTAMP,LOCALTIMESTAMP+1;
+---------------------+-----------------------+
| LOCALTIMESTAMP      | LOCALTIMESTAMP+1      |
+---------------------+-----------------------+
| 2015-04-13 17:17:50 | 20150413171751.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.0">
<title>example1-localtimestamp-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-localtimestamp-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>Displaying current date and time in 
YYYY-MM-DD HH:SS:MM format as well as in YYYYMMDDHHSSMM.uuuuuu format:</h2>
<table class='table table-bordered'>
<tr>
<th>Current date and time in YYYY-MM-DD HH:SS:MM format</th><th>Current date and time in YYYYMMDDHHSSMM.uuuuuu 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 LOCALTIMESTAMP,LOCALTIMESTAMP+1') as $row) {
echo "<tr>";
echo "<td>" . $row['LOCALTIMESTAMP'] . "</td>";
echo "<td>" . $row['LOCALTIMESTAMP+1'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Example: LOCALTIMESTAMP()

The following statement will return the current date and time in ‘YYYY-MM-DD HH:SS:MM’ format.

Code:

SELECT LOCALTIMESTAMP();

Sample Output:

mysql> SELECT LOCALTIMESTAMP();
+---------------------+
| LOCALTIMESTAMP()    |
+---------------------+
| 2015-04-13 17:22:58 | 
+---------------------+
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>example2-localtimestamp-function - php mysql examples | w3resource</title>
<meta name="description" content="example2-localtimestamp-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>Displaying current date and time in 
YYYY-MM-DD HH:SS:MM format:</h2>
<table class='table table-bordered'>
<tr>
<th>Current date and time in YYYY-MM-DD HH:SS:MM 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 LOCALTIMESTAMP()') as $row) {
echo "<tr>";
echo "<td>" . $row['LOCALTIMESTAMP()'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Example : LOCALTIMESTAMP() in numeric format

The following statement will return the current date and time in ‘YYYY-MM-DD HH:SS:MM’ format as well as YYYYMMDDHHSSMM.uuuuuu format.

Code:

SELECT LOCALTIMESTAMP(),LOCALTIMESTAMP()+1;

Sample Output:

mysql> SELECT LOCALTIMESTAMP(),LOCALTIMESTAMP()+1;
+---------------------+-----------------------+
| LOCALTIMESTAMP()    | LOCALTIMESTAMP()+1    |
+---------------------+-----------------------+
| 2015-04-13 17:21:17 | 20150413172118.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.0">
<title>example3-localtimestamp-function - php mysql examples | w3resource</title>
<meta name="description" content="example3-localtimestamp-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>Displaying current date and time in 
YYYY-MM-DD HH:SS:MM format as well as in YYYYMMDDHHSSMM.uuuuuu format:</h2>
<table class='table table-bordered'>
<tr>
<th>Current date and time in YYYY-MM-DD HH:SS:MM format</th><th>Current date and time in YYYYMMDDHHSSMM.uuuuuu 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 LOCALTIMESTAMP(),LOCALTIMESTAMP()+1') as $row) {
echo "<tr>";
echo "<td>" . $row['LOCALTIMESTAMP()'] . "</td>";
echo "<td>" . $row['LOCALTIMESTAMP()+1'] . "</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: LOCALTIME()
Next: MAKEDATE()



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