w3resource logo


MySQL Tutorial

MySQL NOW() function

Secondary Nav

NOW() function

MySQL NOW() 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(), LOCALTIME, LOCALTIME(), LOCALTIMESTAMP, LOCALTIMESTAMP() are synonyms of NOW(). The NOW() returns the constant time when the statement began to work.

Note : If an example code uses NOW(), your output may vary from the output shown.

Syntax :

NOW();

Video Presentation

Pictorial Presentation

Pictorial Presentation of MySQL NOW() function

Example : MySQL NOW() function

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

SELECT NOW();

Output :

mysql> SELECT NOW();
+---------------------+
| NOW()               |
+---------------------+
| 2015-04-14 10:55:19 | 
+---------------------+
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>example-NOW-function - php mysql examples | w3resource</title>
<meta name="description" content="example-NOW-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>Showing current datetime in YYYY-MM-DD HH:SS:MM format using MySQL:</h2>
<table class='table table-bordered'>
<tr>
<th>Current 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 NOW()') as $row) {
echo "<tr>";
echo "<td>" . $row['NOW()'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

JSP script

<[email protected] 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-now-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 NOW()";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Current datetime</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("NOW()")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

Example : NOW() function in numeric format

The following statement will return the current date and time in YYYYMMDDHHSSMM.uuuuuu format.

SELECT NOW(),NOW()+1;

Output :

mysql> SELECT NOW(),NOW()+1;
+---------------------+-----------------------+
| NOW()               | NOW()+1               |
+---------------------+-----------------------+
| 2015-04-14 11:15:32 | 20150414111533.000000 | 
+---------------------+-----------------------+
1 row in set (0.03 sec)

View the example in browser

Example : NOW() function using INTERVAL

The following statement will return the date and current time in ‘YYYY-MM-DD HH:SS:MM’ format for the previous day. The keyword ‘INTERVAL’ have been introduced to get the result.

SELECT NOW(),NOW()-INTERVAL 1 DAY;

Output :

mysql> SELECT NOW(),NOW()-INTERVAL 1 DAY;
+---------------------+----------------------+
| NOW()               | NOW()-INTERVAL 1 DAY |
+---------------------+----------------------+
| 2015-04-14 11:17:25 | 2015-04-13 11:17:25  | 
+---------------------+----------------------+
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-NOW-function - php mysql examples | w3resource</title>
<meta name="description" content="example2-NOW-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>Showing current date and time in YYYY-MM-DD HH:SS:MM and YYYYMMDDHHSSMM.uuuuuu format respectively using MySQL:</h2>
<table class='table table-bordered'>
<tr>
<th>Current datetime in YYYY-MM-DD HH:SS:MM format</th><th>Current datetime for yesterday 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 NOW(),NOW()-INTERVAL 1 DAY') as $row) {
echo "<tr>";
echo "<td>" . $row['NOW()'] . "</td>";
echo "<td>" . $row['NOW()-INTERVAL 1 DAY'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Example : NOW() function using negative INTERVAL

The following statement will return the date and time in ‘YYYY-MM-DD HH:SS:MM’ format before 1 hour of current datetime. The keyword ‘INTERVAL’ have been introduced to get the result.

SELECT NOW(),NOW()-INTERVAL 1 HOUR;

Output :

 mysql> SELECT NOW(),NOW()-INTERVAL 1 HOUR;
+---------------------+-----------------------+
| NOW()               | NOW()-INTERVAL 1 HOUR |
+---------------------+-----------------------+
| 2015-04-14 11:19:01 | 2015-04-14 10:19:01   | 
+---------------------+-----------------------+
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-NOW-function - php mysql examples | w3resource</title>
<meta name="description" content="example3-NOW-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>Showing current date and time in YYYY-MM-DD HH:SS:MM and YYYYMMDDHHSSMM.uuuuuu format respectively using MySQL:</h2>
<table class='table table-bordered'>
<tr>
<th>Current datetime in YYYY-MM-DD HH:SS:MM format</th><th>Current datetime before an hour 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 NOW(),NOW()-INTERVAL 1 HOUR') as $row) {
echo "<tr>";
echo "<td>" . $row['NOW()'] . "</td>";
echo "<td>" . $row['NOW()-INTERVAL 1 HOUR'] . "</td>";
echo "</tr>";  
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Example : NOW() function using INTERVAL for a day

The following statement will return the date and time in ‘YYYY-MM-DD HH:SS:MM’ format after 1 day of the current datetime . The keyword ‘INTERVAL’ have been introduced to get the result.

SELECT NOW(),NOW()+INTERVAL 1 DAY;

Output :

mysql> SELECT NOW(),NOW()+INTERVAL 1 DAY;
+---------------------+----------------------+
| NOW()               | NOW()+INTERVAL 1 DAY |
+---------------------+----------------------+
| 2015-04-14 11:20:42 | 2015-04-15 11:20:42  | 
+---------------------+----------------------+
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>example4-NOW-function - php mysql examples | w3resource</title>
<meta name="description" content="example4-NOW-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>Showing current date and time in YYYY-MM-DD HH:SS:MM and YYYYMMDDHHSSMM.uuuuuu format respectively using MySQL:</h2>
<table class='table table-bordered'>
<tr>
<th>Current datetime in YYYY-MM-DD HH:SS:MM format</th><th>Current datetime after one day 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 NOW(),NOW()+INTERVAL 1 DAY') as $row) {
echo "<tr>";
echo "<td>" . $row['NOW()'] . "</td>";
echo "<td>" . $row['NOW()+INTERVAL 1 DAY'] . "</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.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program