The MySQL Now Function returns the current date and time based on the current system time on the server and the timezone that MySQL is configured to use.
This function is particularly useful when we need to fetch some data from our database, but the data we want must be of a certain age old with respect to the current time.
Note that the MySQL Now function returns the current date and time in this format: 'YYYY-MM-DD HH:MM:SS'.
Have a look at the rest of the tutorial below.
The syntax for the MySQL Now function is as follows:
As you can see above, the function is pretty simple and takes no additional parameters. Let's go on to some examples.
Have a look at the following example of the NOW function at work:
The result will be a date/time in the following format:
Note that you can also use the now function in your SQL as part of a where clause. Look at this:
SELECT * FROM books WHERE due_date < NOW();
The SQL above could be used as a simple check for all overdue library books.
It is important to note that the NOW function returns the time when the SQL statement started to execute, so if you had the following query, the 2 NOW functions would return the exact same time!
SELECT NOW(), SLEEP(2), NOW();
If we wanted to get the exact time when the function itself was executed, we would need to use the SYSDATE function, as in this example:
SELECT SYSDATE(), SLEEP(2), SYSDATE();
Try them out for yourself and see. We hope you found this tutorial on the MySQL Now Function useful.