MySQL中时间戳的字符串

2022-01-13 00:00:00 timestamp sql mysql

有没有办法在 MySQL 中将字符串转换为 UNIX 时间戳?

Is there any way to convert string to UNIX timestamp in MySQL?

例如,我的字符串 2011-12-21 02:20pm 需要采用 Unix 时间戳格式.

For example, I have the string 2011-12-21 02:20pm which needs to be in Unix timestamp format.

推荐答案

UNIX_TIMESTAMP() 解决问题:

SELECT UNIX_TIMESTAMP('2011-12-21 14:20:00');

但是,UNIX_TIMESTAMP() 函数只采用标准 MySQL 格式的日期.如果要使用 AM/PM 表示法,则需要使用 STR_TO_DATE 首先像这样:

However, the UNIX_TIMESTAMP() function only takes a standard MySQL formatted date. If you want to use the AM/PM notation, you will need to use STR_TO_DATE first like this:

SELECT UNIX_TIMESTAMP(
    STR_TO_DATE('2011-12-21 02:20pm', '%Y-%m-%d %h:%i%p')
);

相关文章