如何在 MySQL 中获取当前日期和时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19246309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:04:53  来源:igfitidea点击:

How to get current date & time in MySQL?

mysqlsqldatetime

提问by Samuel Nicholson

Is there a value or command like DATETIME that I can use in a manual query to insert the current date and time?

是否有像 DATETIME 这样的值或命令,我可以在手动查询中使用它来插入当前日期和时间?

INSERT INTO servers (
  server_name, online_status, exchange, disk_space, network_shares
) VALUES(
  'm1', 'ONLINE', 'ONLINE', '100GB', 'ONLINE' 'DATETIME' 
)

The quoted DATETIME value at the end is where I want to add the current date and time.

最后引用的 DATETIME 值是我要添加当前日期和时间的地方。

回答by francisco.preller

You can use NOW():

您可以使用NOW()

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

回答by Ankit Sharma

Use CURRENT_TIMESTAMP()or now()

使用 CURRENT_TIMESTAMP()now()

Like

喜欢

INSERT INTO servers (server_name, online_status, exchange, disk_space,
network_shares,date_time) VALUES('m1','ONLINE','ONLINE','100GB','ONLINE',now() )

or

或者

INSERT INTO servers (server_name, online_status, exchange, disk_space,
network_shares,date_time) VALUES('m1', 'ONLINE', 'ONLINE', '100GB', 'ONLINE'
,CURRENT_TIMESTAMP() )

Replace date_time with the column name you want to use to insert the time.

将 date_time 替换为要用于插入时间的列名。

回答by Erandi

Even though there are many accepted answers, I think this way is also possible:

尽管有很多公认的答案,但我认为这种方式也是可能的:

Create your 'servers'table as following :

创建您的“服务器”表如下

CREATE TABLE `servers`
(
      id int(11) NOT NULL PRIMARY KEY auto_increment,
      server_name varchar(45) NOT NULL,
      online_status varchar(45) NOT NULL,
      _exchange varchar(45) NOT NULL, 
      disk_space varchar(45) NOT NULL,
      network_shares varchar(45) NOT NULL,
      date_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
);

And your INSERTstatement should be :

你的INSERT语句应该是

INSERT INTO servers (server_name, online_status, _exchange, disk_space, network_shares)
VALUES('m1', 'ONLINE', 'ONLINE', '100GB', 'ONLINE');

My Environment:

我的环境:

Core i3 Windows Laptop with 4GB RAM, and I did the above example on MySQL Workbench 6.2 (Version 6.2.5.0 Build 397 64 Bits)

带有 4GB RAM 的 Core i3 Windows 笔记本电脑,我在 MySQL Workbench 6.2(版本 6.2.5.0 Build 397 64 Bits)上做了上面的例子

回答by Alec.

Yes, you can use the CURRENT_TIMESTAMP()command.

是的,您可以使用该CURRENT_TIMESTAMP()命令。

See here: Date and Time Functions

请参阅此处:日期和时间函数

回答by Rajesh Chaurasia

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

回答by Ferhat KO?ER

?f you make change default value to CURRENT_TIMESTAMPit is more effiency,

?如果您将默认值更改CURRENT_TIMESTAMP为更高的效率,

ALTER TABLE servers MODIFY COLUMN network_shares datetime NOT NULL DEFAULT CURRENT_TIMESTAMP;

回答by codeforester

The correct answer is SYSDATE().

正确答案是SYSDATE()

INSERT INTO servers (
    server_name, online_status, exchange, disk_space,
    network_shares, date_time
)
VALUES (
    'm1', 'ONLINE', 'ONLINE', '100GB', 'ONLINE', SYSDATE()
);

We can change this behavior and make MySQL make NOW()behave in the same way as SYSDATE()by setting sysdate_is_nowcommand line argument to True.

我们可以更改此行为并使 MySQL make 的NOW()行为与SYSDATE()sysdate_is_now命令行参数设置为True.

Note that NOW()(which has CURRENT_TIMESTAMP()as an alias), differs from SYSDATE()in a subtleway:

请注意NOW()CURRENT_TIMESTAMP()作为别名),与以下SYSDATE()微妙的不同:

SYSDATE() returns the time at which it executes. This differs from the behavior for NOW(), which returns a constant time that indicates the time at which the statement began to execute. (Within a stored function or trigger, NOW() returns the time at which the function or triggering statement began to execute.)

SYSDATE() 返回它执行的时间。这与 NOW() 的行为不同,后者返回一个常量时间,指示语句开始执行的时间。(在存储的函数或触发器中,NOW() 返回函数或触发语句开始执行的时间。)

As indicated by Erandi, it is best to create your table with the DEFAULTclause so that the column gets populated automatically with the timestamp when you insert a new row:

正如Erandi所指出的,最好使用该DEFAULT子句创建表,以便在插入新行时使用时间戳自动填充该列:

date_time datetime NOT NULL DEFAULT SYSDATE()


If you want the current date in epochformat, then you can use UNIX_TIMESTAMP(). For example:

如果您想要纪元格式的当前日期,那么您可以使用UNIX_TIMESTAMP()。例如:

select now(3), sysdate(3), unix_timestamp();

would yield

会屈服

+-------------------------+-------------------------+------------------+
| now(3)                  | sysdate(3)              | unix_timestamp() |
+-------------------------+-------------------------+------------------+
| 2018-11-27 01:40:08.160 | 2018-11-27 01:40:08.160 |       1543282808 |
+-------------------------+-------------------------+------------------+

Related:

有关的:

回答by tony gil

In database design, iIhighly recommend using Unixtime for consistency and indexing / search / comparison performance.

在数据库设计中,我强烈推荐使用 Unixtime 来保证一致性和索引/搜索/比较性能。

UNIX_TIMESTAMP() 

One can always convert to human readableformats afterwards, internationalizing as is individually most convenient.

之后可以随时转换为人类可读的格式,国际化是最方便的。

FROM_ UNIXTIME (unix_timestamp, [format ])

回答by obohovyk

You can use not only now(), also current_timestamp()and localtimestamp(). The main reason of incorrect display timestamp is inserting NOW() with single quotes! It didn't work for me in MySQL Workbench because of this IDE add single quotes for mysql functions and i didn't recognize it at once )

您不仅可以使用now(),还current_timestamp()localtimestamp()。显示时间戳不正确的主要原因是用单引号插入 NOW() !它在 MySQL Workbench 中对我不起作用,因为此 IDE 为 mysql 函数添加了单引号,但我没有立即识别它)

Don't use functions with single quotes like in MySQL Workbench. It doesn't work.

不要像在 MySQL Workbench 中那样使用带单引号的函数。它不起作用。

回答by user3656636

$rs = $db->Insert('register',"'$fn','$ln','$email','$pass','$city','$mo','$fil'","'f_name','l_name=','email','password','city','contact','image'");