w3resource

MySQL NOW() function

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();

Syntax Diagram:

MySQL NOW() Function - Syntax Diagram

MySQL Version: 5.6


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.

Code:

SELECT NOW();

Sample 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.0">
<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:

<%@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-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.

Code:

SELECT NOW(),NOW()+1;

Sample 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.

Code:

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

Sample 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.0">
<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.

Code:

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

Sample 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.0">
<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.

Code:

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

Sample 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.0">
<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.

Previous: MONTHNAME()
Next: PERIOD_ADD()



Follow us on Facebook and Twitter for latest update.