Getting Current Date-Time In MySQL
You can use several built-in functions to get the current date and time in MySQL. Here are some of the most commonly used ones:
- NOW()
- This function returns the current date and time in the format
YYYY-MM-DD HH:MM:SS
. - Example:
sql SELECT NOW() AS CurrentDateTime;
- CURRENT_TIMESTAMP
- This is equivalent to the
NOW()
function and returns the current date and time in the formatYYYY-MM-DD HH:MM:SS
. - Example:
sql SELECT CURRENT_TIMESTAMP AS CurrentDateTime;
- CURDATE()
- This function returns the current date in the format
YYYY-MM-DD
. - Example:
sql SELECT CURDATE() AS CurrentDate;
- CURTIME()
- This function returns the current time in the format
HH:MM:SS
. - Example:
sql SELECT CURTIME() AS CurrentTime;
- UTC_TIMESTAMP()
- This function returns the current UTC date and time in the format
YYYY-MM-DD HH:MM:SS
. - Example:
sql SELECT UTC_TIMESTAMP() AS CurrentUTCDateTime;
- UTC_DATE()
- This function returns the current UTC date in the format
YYYY-MM-DD
. - Example:
sql SELECT UTC_DATE() AS CurrentUTCDate;
- UTC_TIME()
- This function returns the current UTC time in the format
HH:MM:SS
. - Example:
sql SELECT UTC_TIME() AS CurrentUTCTime;
Here is an example of how you might use one of these functions in a query to insert the current date and time into a table:
INSERT INTO orders (order_id, order_date) VALUES (1, NOW());
This query inserts a new row into the orders
table with an order_id
of 1 and the current date and time as the order_date
.
Please note that the actual format in which the date and time are returned might depend on the settings of your MySQL server.