w3resource

MySQL LOCALTIME() function

LOCALTIME() function

MySQL LOCALTIME returns the value of current date and time in ‘YYYY-MM-DD HH:MM:SS’ format or YYYYMMDDHHMMSS.uuuuuu format depending on the context (numeric or string) of the function. CURRENT_TIMESTAMP, CURRENT_TIMESTAMP(), NOW, NOW(), LOCALTIME(), LOALTIMESTAMP, LOCALTIMESTAMP() are the synonyms of LOCALTIME. The LOCALTIME 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:

LOCALTIME;

Syntax Diagram:

MySQL LOCALTIME() Function - Syntax Diagram

MySQL Version: 5.6


Video Presentation

Pictorial Presentation

Pictorial Presentation of MySQL LOCALTIME() function

Example : MySQL LOCALTIME

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

Code:

SELECT LOCALTIME;

Sample Output:

mysql> SELECT LOCALTIME;
+---------------------+
| LOCALTIME           |
+---------------------+
| 2015-04-13 17:02:09 | 
+---------------------+
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-LOCALTIME-function - php mysql examples | w3resource</title>
<meta name="description" content="example-LOCALTIME-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 LOCALTIME') as $row) {
echo "<tr>"; 
echo "<td>" . $row['LOCALTIME'] . "</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-localtime-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 LOCALTIME";
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("LOCALTIME")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

Example : LOCALTIME 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 LOCALTIME,LOCALTIME+1;

Sample Output:

mysql> SELECT LOCALTIME,LOCALTIME+1;
+---------------------+-----------------------+
| LOCALTIME           | LOCALTIME+1           |
+---------------------+-----------------------+
| 2015-04-13 17:04:24 | 20150413170425.000000 | 
+---------------------+-----------------------+
1 row in set (0.02 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-LOCALTIME-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-LOCALTIME-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 LOCALTIME,LOCALTIME+1') as $row) {
echo "<tr>";
echo "<td>" . $row['LOCALTIME'] . "</td>";
echo "<td>" . $row['LOCALTIME+1'] . "</td>";
echo "</tr>";  
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Example: LOCALTIME()

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

Code:

SELECT LOCALTIME();

Sample Output:

mysql> SELECT LOCALTIME();
+---------------------+
| LOCALTIME()         |
+---------------------+
| 2015-04-13 17:06:19 | 
+---------------------+
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>example2-LOCALTIME-function - php mysql examples | w3resource</title>
<meta name="description" content="example2-LOCALTIME-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 LOCALTIME()') as $row) {
echo "<tr>";
echo "<td>" . $row['LOCALTIME()'] . "</td>";
echo "</tr>"; 
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Example: LOCALTIME() 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.

SELECT LOCALTIME(),LOCALTIME()+1;

Sample Output:

mysql> SELECT LOCALTIME(),LOCALTIME()+1;
+---------------------+-----------------------+
| LOCALTIME()         | LOCALTIME()+1         |
+---------------------+-----------------------+
| 2015-04-13 17:08:12 | 20150413170813.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>example3-LOCALTIME-function - php mysql examples | w3resource</title>
<meta name="description" content="example3-LOCALTIME-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 LOCALTIME(),LOCALTIME()+1') as $row) {echo "<tr>"; 
echo "<td>" . $row['LOCALTIME()'] . "</td>";
echo "<td>" . $row['LOCALTIME()+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: LAST_DAY()
Next: LOCALTIMESTAMP()