How to get current date in MySQL

MySQL is a popular open-source relational database management system that is widely used in web development. It provides a wide range of features that make it a versatile and powerful tool for developers. One of the most common tasks in MySQL is to get the current date. In this article, we will explore different ways to get the current date in MySQL.

Method 1: Using the NOW() Function

The easiest way to get the current date in MySQL is to use the NOW() function. This function returns the current date and time in the ‘YYYY-MM-DD HH:MM:SS’ format. Here is an example:

SELECT NOW();

The output will be something like this:

2022-06-09 12:34:56

Note that the NOW() function uses the server’s timezone, which may not be the same as the timezone of the user. If you need to use a different timezone, you can use the CONVERT_TZ() function to convert the date to the desired timezone. Here is an example:

SELECT CONVERT_TZ(NOW(), ‘+00:00’, ‘+05:30’);

This will return the current date and time in the Indian Standard Time (IST) timezone.

Method 2: Using the CURDATE() Function

Another way to get the current date in MySQL is to use the CURDATE() function. This function returns the current date in the ‘YYYY-MM-DD’ format. Here is an example:

SELECT CURDATE();

The output will be something like this:

2022-06-09

Note that the CURDATE() function does not include the time component. If you need the current time as well, you can combine the CURDATE() function with the TIME() function. Here is an example:

SELECT CONCAT(CURDATE(), ‘ ‘, TIME(NOW()));

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

Method 3: Using the DATE() Function

The DATE() function is another way to get the current date in MySQL. This function extracts the date part from a datetime expression. Here is an example:

SELECT DATE(NOW());

The output will be something like this:

2022-06-09

Note that the DATE() function is equivalent to the CURDATE() function. You can use either of them depending on your preference.

Method 4: Using the SYSDATE() Function

The SYSDATE() function is similar to the NOW() function in that it returns the current date and time. However, SYSDATE() is an Oracle-compatible function that is available in MySQL for compatibility purposes. Here is an example:

SELECT SYSDATE();

The output will be something like this:

2022-06-09 12:34:56

Note that the SYSDATE() function uses the system timezone, which may not be the same as the timezone of the user. If you need to use a different timezone, you can use the CONVERT_TZ() function as described in Method 1.

Method 5: Using the CURRENT_DATE() Function

The CURRENT_DATE() function is another way to get the current date in MySQL. This function returns the current date in the ‘YYYY-MM-DD’ format. Here is an example:

SELECT CURRENT_DATE();

The output will be something like this:

2022-06-09

Note that the CURRENT_DATE() function is equivalent to the CURDATE() function. You can use either of them depending on your preference.

In this article, we have explored different ways to get the current date in MySQL. We have discussed the NOW(), CURDATE(), DATE(), SYSDATE(), and CURRENT_DATE() functions. Depending on your requirements, you can choose the appropriate function to get the current date and time in the desired format. It is important to understand the differences between these functions and how they handle timezones. With this knowledge, you can use MySQL effectively to manage dates and times in your applications.