Home > Database > Mysql Tutorial > body text

Insert current date in datetime format MySQL?

王林
Release: 2023-08-28 22:33:02
forward
1501 people have browsed it

以日期时间格式插入当前日期 MySQL?

To insert the current date (not the time), you can use MySQL's built-in function CURDATE(). The syntax is as follows -

INSERT INTO yourTableName values(curdate());
Copy after login

Alternatively, if you want to add date and time, then you can use MySQL's built-in function NOW(). The syntax is as follows -

INSERT INTO yourTableName values(now());
Copy after login

To understand these two syntaxes, we first create a table. The query to create the table is as follows -

mysql> create table NowAndCurdateDemo
   −> (
   −> YourDueDate datetime
   −> );
Query OK, 0 rows affected (1.75 sec)
Copy after login

Implement the function to insert the current date and date time into the table. The query to insert date is as follows -

mysql> insert into NowAndCurdateDemo values(curdate());
Query OK, 1 row affected (0.28 sec)

mysql> insert into NowAndCurdateDemo values(now());
Query OK, 1 row affected (0.14 sec)
Copy after login

Use the following query to check if the data is inserted into the table -

mysql> select *from NowAndCurdateDemo;
Copy after login

The following is the output -

+---------------------+
| YourDueDate         |
+---------------------+
| 2018-12-05 00:00:00 |
| 2018-12-05 21:24:10 |
+---------------------+
2 rows in set (0.06 sec)
Copy after login

The above is the detailed content of Insert current date in datetime format MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!