Home >Database >Mysql Tutorial >How Can I Get the Current Date and Time in MySQL?

How Can I Get the Current Date and Time in MySQL?

Susan Sarandon
Susan SarandonOriginal
2024-12-16 02:08:10590browse

How Can I Get the Current Date and Time in MySQL?

Obtaining Current Date and Time in MySQL

You may want to insert the current date and time when adding new records to your MySQL database. This allows for accurate timestamping and tracking of record creation.

Solution Using NOW()

To insert the current date and time into a MySQL query, you can utilize the NOW() function. This function returns a string representing the current date and time according to the server's time zone.

To demonstrate its usage, consider the example provided:

INSERT INTO servers (
  server_name, online_status, exchange, disk_space, network_shares, c_time
)
VALUES(
  'm1', 'ONLINE', 'exchange', 'disk_space', 'network_shares', NOW()
)

In this query, the NOW() function is used to insert the current date and time into the c_time column of the servers table. The c_time column would then contain the exact moment the record was created, allowing for precise tracking.

The above is the detailed content of How Can I Get the Current Date and Time in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn