在 MySQL 中将日期转换为毫秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28563195/
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 13:21:26  来源:igfitidea点击:

Convert Date to Milliseconds in MySQL

mysqldatemilliseconds

提问by Piko

I am trying to convert a date in MySQLto milliseconds. This is what I have to get the date:

我正在尝试将日期转换MySQLmilliseconds. 这是我必须得到的日期:

DATE_ADD(mydate, INTERVAL(1-DAYOFWEEK(mydate)) DAY)

But that returns me like 15/02/2015and I want to get the millisecondsof that date.

但这让我喜欢15/02/2015,我想得到milliseconds那个日期。

回答by Patrick

Use the UNIX_TIMESTAMP function.

使用 UNIX_TIMESTAMP 函数。

SELECT (UNIX_TIMESTAMP(mydate)*1000) FROM...

SELECT (UNIX_TIMESTAMP(mydate)*1000) FROM...

UNIX_TIMESTAMP will get you seconds and you need to multiply by 1000 to get milliseconds.

UNIX_TIMESTAMP 将为您提供秒数,您需要乘以 1000 才能得到毫秒数。

To convert back, use FROM_UNIXTIME() function.

要转换回来,请使用 FROM_UNIXTIME() 函数。

SELECT FROM_UNIXTIME(date_in_milliseconds/1000) FROM ...

SELECT FROM_UNIXTIME(date_in_milliseconds/1000) FROM ...

Again, you need to divide by 1000 to get it to seconds before using the function.

同样,在使用该函数之前,您需要除以 1000 以得到秒数。