{"id":2615,"date":"2013-05-21T18:24:20","date_gmt":"2013-05-22T02:24:20","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=2615"},"modified":"2023-12-30T07:41:02","modified_gmt":"2023-12-30T14:41:02","slug":"mysql-now-function","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-now-function\/","title":{"rendered":"MySQL NOW() Function"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the MySQL <code>NOW()<\/code>&nbsp;function to get the current date and time of the server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to MySQL NOW() function<\/h2>\n\n\n\n<p>The MySQL <code>NOW()<\/code> function returns the current date and time in the configured time zone as a string or a number in the <code>'YYYY-MM-DD HH:MM:DD'<\/code> or <code>'YYYYMMDDHHMMSS.uuuuuu'<\/code> format.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>NOW()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">NOW()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The returned type of the <code>NOW()<\/code> function depends on the context where you use it. <\/p>\n\n\n\n<p>For example, in the following statement, the <code>NOW()<\/code> function returns the current date and time as a string:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">NOW<\/span>();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-date-functions\/mysql-now-function\/#1\" target=\"_blank\" rel=\"noopener noreferrer\">Try It Out<\/a><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+---------------------+\r\n| NOW()               |\r\n+---------------------+\r\n| <span class=\"hljs-number\">2023<\/span><span class=\"hljs-number\">-12<\/span><span class=\"hljs-number\">-30<\/span> <span class=\"hljs-number\">21<\/span>:<span class=\"hljs-number\">10<\/span>:<span class=\"hljs-number\">54<\/span> |\r\n+---------------------+\r\n<span class=\"hljs-number\">1<\/span> row <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, in the numeric context, the <code>NOW()<\/code> function returns the current date and time as a number as shown in the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">NOW<\/span>() + <span class=\"hljs-number\">0<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-date-functions\/mysql-now-function\/#2\" target=\"_blank\" rel=\"noopener noreferrer\">Try It Out<\/a><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+----------------+\r\n| NOW() + <span class=\"hljs-number\">0<\/span>      |\r\n+----------------+\r\n| <span class=\"hljs-number\">20231230211120<\/span> |\r\n+----------------+\r\n<span class=\"hljs-number\">1<\/span> row <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Notice that the <code>NOW()<\/code> function returns the date and time at which the statement started executing. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">NOW<\/span>(), <span class=\"hljs-keyword\">SLEEP<\/span>(<span class=\"hljs-number\">5<\/span>), <span class=\"hljs-keyword\">NOW<\/span>();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-date-functions\/mysql-now-function\/#3\" target=\"_blank\" rel=\"noopener noreferrer\">Try It Out<\/a><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+---------------------+----------+---------------------+\r\n| NOW()               | SLEEP(<span class=\"hljs-number\">5<\/span>) | NOW()               |\r\n+---------------------+----------+---------------------+\r\n| <span class=\"hljs-number\">2023<\/span><span class=\"hljs-number\">-12<\/span><span class=\"hljs-number\">-30<\/span> <span class=\"hljs-number\">21<\/span>:<span class=\"hljs-number\">11<\/span>:<span class=\"hljs-number\">36<\/span> |        <span class=\"hljs-number\">0<\/span> | <span class=\"hljs-number\">2023<\/span><span class=\"hljs-number\">-12<\/span><span class=\"hljs-number\">-30<\/span> <span class=\"hljs-number\">21<\/span>:<span class=\"hljs-number\">11<\/span>:<span class=\"hljs-number\">36<\/span> |\r\n+---------------------+----------+---------------------+\r\n<span class=\"hljs-number\">1<\/span> row <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (5.02 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the query, the first <code>NOW()<\/code> function executed, and the <code>SLEEP(5)<\/code> function paused the execution of the query for 5 seconds, and the second NOW() function executed. <\/p>\n\n\n\n<p>However, both <code>NOW()<\/code> functions return the same value, even when they are executed at different times.<\/p>\n\n\n\n<p>If you want to get the exact time at which the statement executes, you use the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-sysdate\/\"><code>SYSDATE()<\/code><\/a> function instead. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">SYSDATE<\/span>(), <span class=\"hljs-keyword\">SLEEP<\/span>(<span class=\"hljs-number\">5<\/span>), <span class=\"hljs-keyword\">SYSDATE<\/span>();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-date-functions\/mysql-now-function\/#4\" target=\"_blank\" rel=\"noopener noreferrer\">Try It Out<\/a><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+---------------------+----------+---------------------+\r\n| SYSDATE()           | SLEEP(<span class=\"hljs-number\">5<\/span>) | SYSDATE()           |\r\n+---------------------+----------+---------------------+\r\n| <span class=\"hljs-number\">2023<\/span><span class=\"hljs-number\">-12<\/span><span class=\"hljs-number\">-30<\/span> <span class=\"hljs-number\">21<\/span>:<span class=\"hljs-number\">11<\/span>:<span class=\"hljs-number\">59<\/span> |        <span class=\"hljs-number\">0<\/span> | <span class=\"hljs-number\">2023<\/span><span class=\"hljs-number\">-12<\/span><span class=\"hljs-number\">-30<\/span> <span class=\"hljs-number\">21<\/span>:<span class=\"hljs-number\">12<\/span>:<span class=\"hljs-number\">04<\/span> |\r\n+---------------------+----------+---------------------+\r\n<span class=\"hljs-number\">1<\/span> row <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (5.01 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you want to change the MySQL server&#8217;s time zone to adjust the current date and time returned by the <code>NOW()<\/code> function, you use the following statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SET<\/span> <span class=\"hljs-keyword\">time_zone<\/span> = your_time_zone;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">MySQL NOW() function calculations<\/h2>\n\n\n\n<p>Because the <code>NOW()<\/code> function returns a number when it is used in a numeric context, you can use it in calculations e.g., now plus 1 hour, now minus 1 hour, and now plus 1 day.<\/p>\n\n\n\n<p>The following statement returns the current date and time, now minus 1 hour and now plus 1 hour:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-comment\">-- mysql now minus 1 hour<\/span>\n<span class=\"hljs-keyword\">SELECT<\/span> \n  (<span class=\"hljs-keyword\">NOW<\/span>() - <span class=\"hljs-built_in\">INTERVAL<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">HOUR<\/span>) <span class=\"hljs-string\">'NOW - 1 hour'<\/span>, \n  <span class=\"hljs-keyword\">NOW<\/span>(), \n<span class=\"hljs-comment\">-- mysql now plus 1 hour<\/span>\n  <span class=\"hljs-keyword\">NOW<\/span>() + <span class=\"hljs-built_in\">INTERVAL<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">HOUR<\/span> <span class=\"hljs-string\">'NOW + 1 hour'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-date-functions\/mysql-now-function\/#5\" target=\"_blank\" rel=\"noopener noreferrer\">Try It Out<\/a><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+---------------------+---------------------+---------------------+\r\n| NOW - <span class=\"hljs-number\">1<\/span> hour        | NOW()               | NOW + <span class=\"hljs-number\">1<\/span> hour        |\r\n+---------------------+---------------------+---------------------+\r\n| <span class=\"hljs-number\">2023<\/span><span class=\"hljs-number\">-12<\/span><span class=\"hljs-number\">-30<\/span> <span class=\"hljs-number\">20<\/span>:<span class=\"hljs-number\">12<\/span>:<span class=\"hljs-number\">23<\/span> | <span class=\"hljs-number\">2023<\/span><span class=\"hljs-number\">-12<\/span><span class=\"hljs-number\">-30<\/span> <span class=\"hljs-number\">21<\/span>:<span class=\"hljs-number\">12<\/span>:<span class=\"hljs-number\">23<\/span> | <span class=\"hljs-number\">2023<\/span><span class=\"hljs-number\">-12<\/span><span class=\"hljs-number\">-30<\/span> <span class=\"hljs-number\">22<\/span>:<span class=\"hljs-number\">12<\/span>:<span class=\"hljs-number\">23<\/span> |\r\n+---------------------+---------------------+---------------------+\r\n<span class=\"hljs-number\">1<\/span> row <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following statement returns the current date and time, now minus 1 day and now plus 1 day:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> (<span class=\"hljs-keyword\">NOW<\/span>() - <span class=\"hljs-built_in\">INTERVAL<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">DAY<\/span>) <span class=\"hljs-string\">'NOW - 1 day'<\/span>,\n        <span class=\"hljs-keyword\">NOW<\/span>(),\n        (<span class=\"hljs-keyword\">NOW<\/span>() + <span class=\"hljs-built_in\">INTERVAL<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">DAY<\/span>) <span class=\"hljs-string\">'NOW + 1 day'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-date-functions\/mysql-now-function\/#6\" target=\"_blank\" rel=\"noopener noreferrer\">Try It Out<\/a><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+---------------------+---------------------+---------------------+\r\n| NOW - <span class=\"hljs-number\">1<\/span> day         | NOW()               | NOW + <span class=\"hljs-number\">1<\/span> day         |\r\n+---------------------+---------------------+---------------------+\r\n| <span class=\"hljs-number\">2023<\/span><span class=\"hljs-number\">-12<\/span><span class=\"hljs-number\">-29<\/span> <span class=\"hljs-number\">21<\/span>:<span class=\"hljs-number\">12<\/span>:<span class=\"hljs-number\">37<\/span> | <span class=\"hljs-number\">2023<\/span><span class=\"hljs-number\">-12<\/span><span class=\"hljs-number\">-30<\/span> <span class=\"hljs-number\">21<\/span>:<span class=\"hljs-number\">12<\/span>:<span class=\"hljs-number\">37<\/span> | <span class=\"hljs-number\">2023<\/span><span class=\"hljs-number\">-12<\/span><span class=\"hljs-number\">-31<\/span> <span class=\"hljs-number\">21<\/span>:<span class=\"hljs-number\">12<\/span>:<span class=\"hljs-number\">37<\/span> |\r\n+---------------------+---------------------+---------------------+\r\n<span class=\"hljs-number\">1<\/span> row <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Using the NOW() function as the default value for a column<\/h2>\n\n\n\n<p>You can use the <code>NOW()<\/code> function to provide a default value for a <code>DATETIME<\/code> or <code>TIMESTAMP<\/code> column. <\/p>\n\n\n\n<p>When you omit the date or time value in the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\">INSERT<\/a><\/code> statement, MySQL automatically inserts the current date and time into the column whose default value is <code>NOW()<\/code>. For example:<\/p>\n\n\n\n<p>First, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">create a new table<\/a> called <code>contacts<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> contacts(\r\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span> AUTO_INCREMENT,\r\n    <span class=\"hljs-keyword\">name<\/span> <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\r\n    email <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span> <span class=\"hljs-keyword\">UNIQUE<\/span>,\r\n    created_at DATETIME <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-keyword\">NOW<\/span>(),\r\n    updated_at DATETIME <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-keyword\">NOW<\/span>() <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">UPDATE<\/span> <span class=\"hljs-keyword\">NOW<\/span>()\r\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>contacts<\/code> table includes the following columns: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>id<\/code> : This is the primary key column of the contacts table.<\/li>\n\n\n\n<li><code>name<\/code>: This represents the name of the contact.<\/li>\n\n\n\n<li><code>email<\/code>: This column stores the contact&#8217;s email.<\/li>\n\n\n\n<li><code>created_on<\/code> : This column has the default value specified by the <code>NOW()<\/code> function.<\/li>\n\n\n\n<li><code>updated_at<\/code>: This column sets <code>updated_at<\/code> with a default value of the current date and time when a new row is inserted. Additionally, the <code>ON UPDATE NOW()<\/code> clause ensures that the column is automatically updated to the current date and tie whenever the row is updated.<\/li>\n<\/ul>\n\n\n\n<p class=\"note\">Notice that <code>CURRENT_TIMESTAMP<\/code> and <code>CURRENT_TIMESTAMP()<\/code> are synonyms for <code>NOW()<\/code> so you can use them interchangeably.<\/p>\n\n\n\n<p>Second, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\">insert a new row<\/a> into the <code>contacts<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> contacts(<span class=\"hljs-keyword\">name<\/span>, email)\r\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'Jane Doe'<\/span>, <span class=\"hljs-string\">'john.doe@example.com'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, retrieve data from the <code>contacts<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> contacts;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+----+----------+----------------------+---------------------+---------------------+\r\n| id | name     | email                | created_at          | updated_at          |\r\n+----+----------+----------------------+---------------------+---------------------+\r\n|  <span class=\"hljs-number\">1<\/span> | Jane Doe | john.doe@example.com | <span class=\"hljs-number\">2023<\/span><span class=\"hljs-number\">-12<\/span><span class=\"hljs-number\">-30<\/span> <span class=\"hljs-number\">21<\/span>:<span class=\"hljs-number\">36<\/span>:<span class=\"hljs-number\">09<\/span> | <span class=\"hljs-number\">2023<\/span><span class=\"hljs-number\">-12<\/span><span class=\"hljs-number\">-30<\/span> <span class=\"hljs-number\">21<\/span>:<span class=\"hljs-number\">36<\/span>:<span class=\"hljs-number\">09<\/span> |\r\n+----+----------+----------------------+---------------------+---------------------+\r\n<span class=\"hljs-number\">1<\/span> row <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the <code>INSERT<\/code> statement, we don&#8217;t provide the values for the <code>created_at<\/code> and <code>updated_at<\/code> columns. Therefore, they use the default value which is the current date and time returned by the NOW() function.<\/p>\n\n\n\n<p>Fourth, update the row with id 1:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">UPDATE contacts\r\nSET name = <span class=\"hljs-string\">'Jane Smith'<\/span>\r\nWHERE id = <span class=\"hljs-number\">1<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the UPDATE statement, we don&#8217;t specify the value for the updated_at column so it is updated to the time the row is updated.<\/p>\n\n\n\n<p>Finally, retrieve the data from the contacts table:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">SELECT * FROM contacts\nWHERE id = 1;<\/code><\/span><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+----+------------+----------------------+---------------------+---------------------+\r\n| id | name       | email                | created_at          | updated_at          |\r\n+----+------------+----------------------+---------------------+---------------------+\r\n|  <span class=\"hljs-number\">1<\/span> | Jane Smith | john.doe@example.com | <span class=\"hljs-number\">2023<\/span><span class=\"hljs-number\">-12<\/span><span class=\"hljs-number\">-30<\/span> <span class=\"hljs-number\">21<\/span>:<span class=\"hljs-number\">36<\/span>:<span class=\"hljs-number\">09<\/span> | <span class=\"hljs-number\">2023<\/span><span class=\"hljs-number\">-12<\/span><span class=\"hljs-number\">-30<\/span> <span class=\"hljs-number\">21<\/span>:<span class=\"hljs-number\">37<\/span>:<span class=\"hljs-number\">21<\/span> |\r\n+----+------------+----------------------+---------------------+---------------------+\r\n<span class=\"hljs-number\">1<\/span> row <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>NOW()<\/code> function to get the current date and time of the server.<\/li>\n\n\n\n<li>Use the <code>DEFAULT NOW()<\/code> for a column to set the default value for the column to the current date and time when a row is inserted.<\/li>\n\n\n\n<li>Use the <code>ON UPDATE NOW()<\/code> for a column to set a default for a column to the current date and time when a row is updated.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2615\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-now-function\/\"\n\t\t\t\tdata-post-title=\"MySQL NOW() Function\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2615\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-now-function\/\"\n\t\t\t\tdata-post-title=\"MySQL NOW() Function\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to apply MySQL NOW() function that returns the current date and time at which the statement started executing.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":7032,"menu_order":2,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-2615","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL NOW() Function<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to apply MySQL NOW() function that returns the current date and time at which the statement started executing.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-now-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL NOW() Function\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to apply MySQL NOW() function that returns the current date and time at which the statement started executing.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-now-function\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-30T14:41:02+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-date-functions\\\/mysql-now-function\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-date-functions\\\/mysql-now-function\\\/\",\"name\":\"MySQL NOW() Function\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"datePublished\":\"2013-05-22T02:24:20+00:00\",\"dateModified\":\"2023-12-30T14:41:02+00:00\",\"description\":\"This tutorial shows you how to apply MySQL NOW() function that returns the current date and time at which the statement started executing.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-date-functions\\\/mysql-now-function\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-date-functions\\\/mysql-now-function\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-date-functions\\\/mysql-now-function\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Date Functions\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-date-functions\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL NOW() Function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL NOW() Function","description":"This tutorial shows you how to apply MySQL NOW() function that returns the current date and time at which the statement started executing.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-now-function\/","og_locale":"en_US","og_type":"article","og_title":"MySQL NOW() Function","og_description":"This tutorial shows you how to apply MySQL NOW() function that returns the current date and time at which the statement started executing.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-now-function\/","og_site_name":"MySQL Tutorial","article_modified_time":"2023-12-30T14:41:02+00:00","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-now-function\/","url":"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-now-function\/","name":"MySQL NOW() Function","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"datePublished":"2013-05-22T02:24:20+00:00","dateModified":"2023-12-30T14:41:02+00:00","description":"This tutorial shows you how to apply MySQL NOW() function that returns the current date and time at which the statement started executing.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-now-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-now-function\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-now-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Date Functions","item":"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/"},{"@type":"ListItem","position":3,"name":"MySQL NOW() Function"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=2615"}],"version-history":[{"count":4,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2615\/revisions"}],"predecessor-version":[{"id":13910,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2615\/revisions\/13910"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/7032"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=2615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}