Close

2023-08-17

Getting Current Date-Time In MySQL

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:

  1. NOW()
  • This function returns the current date and time in the format YYYY-MM-DD HH:MM:SS.
  • Example:
    sql SELECT NOW() AS CurrentDateTime;
  1. CURRENT_TIMESTAMP
  • This is equivalent to the NOW() function and returns the current date and time in the format YYYY-MM-DD HH:MM:SS.
  • Example:
    sql SELECT CURRENT_TIMESTAMP AS CurrentDateTime;
  1. CURDATE()
  • This function returns the current date in the format YYYY-MM-DD.
  • Example:
    sql SELECT CURDATE() AS CurrentDate;
  1. CURTIME()
  • This function returns the current time in the format HH:MM:SS.
  • Example:
    sql SELECT CURTIME() AS CurrentTime;
  1. 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;
  1. UTC_DATE()
  • This function returns the current UTC date in the format YYYY-MM-DD.
  • Example:
    sql SELECT UTC_DATE() AS CurrentUTCDate;
  1. 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.