MySQL基础知识04数据类型日期时间的格式转换

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL基础知识04数据类型日期时间的格式转换相关的知识,希望对你有一定的参考价值。

1. UNIX_TIMESTAMP

mysql提供了UNIX_TIMESTAMP()函数,用于计算自从1970-01-01 08:00:00以来所经过的秒数。此处开始时间是1970-01-01 08:00:00,而不是1970-01-01 00:00:00。对于早于1970-01-01 08:00:00的时间,返回值都为0。这个相对时间单位为秒,支持小数。

 

mysql> select unix_timestamp();

+------------------+

| unix_timestamp() |

+------------------+

|       1504246140 |

+------------------+

1 row in set (0.00 sec)

 

mysql> select unix_timestamp(‘1970-01-01 00:00:00‘);

+---------------------------------------+

| unix_timestamp(‘1970-01-01 00:00:00‘) |

+---------------------------------------+

|                                     0 |

+---------------------------------------+

1 row in set (0.00 sec)

 

mysql> select unix_timestamp(‘1970-01-01 01:00:00‘);

+---------------------------------------+

| unix_timestamp(‘1970-01-01 01:00:00‘) |

+---------------------------------------+

|                                     0 |

+---------------------------------------+

1 row in set (0.00 sec)

 

 

mysql> select unix_timestamp(‘1970-01-01 08:00:00‘);

+---------------------------------------+

| unix_timestamp(‘1970-01-01 08:00:00‘) |

+---------------------------------------+

|                                     0 |

+---------------------------------------+

1 row in set (0.00 sec)

 

mysql> select unix_timestamp(‘1970-01-01 08:00:01‘);

+---------------------------------------+

| unix_timestamp(‘1970-01-01 08:00:01‘) |

+---------------------------------------+

|                                     1 |

+---------------------------------------+

1 row in set (0.00 sec)

 

mysql> select unix_timestamp(‘1970-01-01 08:00:02‘);

+---------------------------------------+

| unix_timestamp(‘1970-01-01 08:00:02‘) |

+---------------------------------------+

|                                     2 |

+---------------------------------------+

1 row in set (0.00 sec)

 

 

mysql> select unix_timestamp(‘1970-01-02 00:00:00‘);

+---------------------------------------+

| unix_timestamp(‘1970-01-02 00:00:00‘) |

+---------------------------------------+

|                                 57600 |

+---------------------------------------+

1 row in set (0.00 sec)

 

mysql> select unix_timestamp(‘1970-01-03 00:00:00‘);

+---------------------------------------+

| unix_timestamp(‘1970-01-03 00:00:00‘) |

+---------------------------------------+

|                                144000 |

+---------------------------------------+

1 row in set (0.00 sec)

 

 

unix_timestamp()函数支持小数,但最多保留6位小数。

mysql> select unix_timestamp(‘1970-01-01 08:00:02.123456‘);

+----------------------------------------------+

| unix_timestamp(‘1970-01-01 08:00:02.123456‘) |

+----------------------------------------------+

|                                     2.123456 |

+----------------------------------------------+

1 row in set (0.00 sec)

 

mysql> select unix_timestamp(‘1970-01-01 08:00:02.123456789‘);

+-------------------------------------------------+

| unix_timestamp(‘1970-01-01 08:00:02.123456789‘) |

+-------------------------------------------------+

|                                        2.123457 |

+-------------------------------------------------+

1 row in set (0.00 sec)

 

 

2. FROM_UNIXTIME

 

FROM_UNIXTIME()函数将自1970-01-01 08:00:00以来的秒数,转化为标准的字符串时间戳格式。

mysql> select from_unixtime( 0 );

+---------------------+

| from_unixtime( 0 )  |

+---------------------+

| 1970-01-01 08:00:00 |

+---------------------+

1 row in set (0.00 sec)

 

mysql> select from_unixtime( 1 );

+---------------------+

| from_unixtime( 1 )  |

+---------------------+

| 1970-01-01 08:00:01 |

+---------------------+

1 row in set (0.00 sec)

 

mysql> select from_unixtime( 1.123456 );

+----------------------------+

| from_unixtime( 1.123456 )  |

+----------------------------+

| 1970-01-01 08:00:01.123456 |

+----------------------------+

1 row in set (0.00 sec)

 

FROM_UNIXTIME()函数支持日期时间字符串的格式化。

mysql> select from_unixtime( 1.123456 ,‘%Y-%m-%d %H:%i:%s‘);

+-----------------------------------------------+

| from_unixtime( 1.123456 ,‘%Y-%m-%d %H:%i:%s‘) |

+-----------------------------------------------+

| 1970-01-01 08:00:01                           |

+-----------------------------------------------+

1 row in set (0.00 sec)

 

 

3. DATE_FORMAT

将日期和时间相关类型转化为指定格式的字符串。

mysql> select date_format( now(),‘%Y-%m-%d %H:%i:%s‘);

+-----------------------------------------+

| date_format( now(),‘%Y-%m-%d %H:%i:%s‘) |

+-----------------------------------------+

| 2017-09-01 14:35:22                     |

+-----------------------------------------+

1 row in set (0.00 sec)

 

 

4. SEC_TO_TIME

SEC_TO_TIME()函数将秒数转化为时间。

 

mysql> select sec_to_time(0),sec_to_time(1),sec_to_time(3600),sec_to_time(36000),sec_to_time(360000);

+----------------+----------------+-------------------+--------------------+---------------------+

| sec_to_time(0) | sec_to_time(1) | sec_to_time(3600) | sec_to_time(36000) | sec_to_time(360000) |

+----------------+----------------+-------------------+--------------------+---------------------+

| 00:00:00       | 00:00:01       | 01:00:00          | 10:00:00           | 100:00:00           |

+----------------+----------------+-------------------+--------------------+---------------------+

1 row in set (0.00 sec)

 

 

5. TIME_TO_SEC

TIME_TO_SEC()函数将时间转化为秒数。

 

mysql> select time_to_sec(‘00:00:00‘), time_to_sec(‘00:00:01‘),time_to_sec(‘01:00:00‘),time_to_sec(‘100:00:00‘);

+-------------------------+-------------------------+-------------------------+--------------------------+

| time_to_sec(‘00:00:00‘) | time_to_sec(‘00:00:01‘) | time_to_sec(‘01:00:00‘) | time_to_sec(‘100:00:00‘) |

+-------------------------+-------------------------+-------------------------+--------------------------+

|                       0 |                       1 |                    3600 |                   360000 |

+-------------------------+-------------------------+-------------------------+--------------------------+

1 row in set (0.00 sec)

 

 

 

 

6. TIMESTAMPDIFF

 

TIMESTAMPDIFF()函数用于计算两个时间戳之间的差异,单位可以指定为年月日时分秒中的任何一个,也可以指定为微秒。

mysql> select timestampdiff(YEAR,‘2017-09-01‘, ‘2000-09-01‘);

+------------------------------------------------+

| timestampdiff(YEAR,‘2017-09-01‘, ‘2000-09-01‘) |

+------------------------------------------------+

|                                            -17 |

+------------------------------------------------+

1 row in set (0.00 sec)

 

mysql> select timestampdiff(YEAR,‘2017-09-01‘, ‘2027-09-01‘);

+------------------------------------------------+

| timestampdiff(YEAR,‘2017-09-01‘, ‘2027-09-01‘) |

+------------------------------------------------+

|                                             10 |

+------------------------------------------------+

1 row in set (0.00 sec)

 

mysql> select timestampdiff(MONTH,‘2017-09-01‘, ‘2027-09-01‘);

+-------------------------------------------------+

| timestampdiff(MONTH,‘2017-09-01‘, ‘2027-09-01‘) |

+-------------------------------------------------+

|                                             120 |

+-------------------------------------------------+

1 row in set (0.00 sec)

 

mysql> select timestampdiff(DAY,‘2017-09-01‘, ‘2017-10-01‘);

+-----------------------------------------------+

| timestampdiff(DAY,‘2017-09-01‘, ‘2017-10-01‘) |

+-----------------------------------------------+

|                                            30 |

+-----------------------------------------------+

1 row in set (0.00 sec)

 

 

mysql> select timestampdiff(HOUR,‘2017-09-01‘, ‘2017-10-01‘);

+------------------------------------------------+

| timestampdiff(HOUR,‘2017-09-01‘, ‘2017-10-01‘) |

+------------------------------------------------+

|                                            720 |

+------------------------------------------------+

1 row in set (0.00 sec)

 

mysql> select timestampdiff(MINUTE,‘2017-09-01‘, ‘2017-10-01‘);

+--------------------------------------------------+

| timestampdiff(MINUTE,‘2017-09-01‘, ‘2017-10-01‘) |

+--------------------------------------------------+

|                                            43200 |

+--------------------------------------------------+

1 row in set (0.00 sec)

 

mysql> select timestampdiff(SECOND,‘2017-09-01‘, ‘2017-10-01‘);

+--------------------------------------------------+

| timestampdiff(SECOND,‘2017-09-01‘, ‘2017-10-01‘) |

+--------------------------------------------------+

|                                          2592000 |

+--------------------------------------------------+

1 row in set (0.00 sec)

 

mysql> select timestampdiff(MICROSECOND,‘2017-09-01‘, ‘2017-10-01‘);

+-------------------------------------------------------+

| timestampdiff(MICROSECOND,‘2017-09-01‘, ‘2017-10-01‘) |

+-------------------------------------------------------+

|                                         2592000000000 |

+-------------------------------------------------------+

1 row in set (0.00 sec)

 

mysql> select timestampdiff(MICROSECOND,‘2017-09-01 00:00:00.123456‘, ‘2017-09-01 00:00:01.000001‘);

+---------------------------------------------------------------------------------------+

| timestampdiff(MICROSECOND,‘2017-09-01 00:00:00.123456‘, ‘2017-09-01 00:00:01.000001‘) |

+---------------------------------------------------------------------------------------+

|                                                                                876545 |

+---------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

以上是关于MySQL基础知识04数据类型日期时间的格式转换的主要内容,如果未能解决你的问题,请参考以下文章

将访问日期时间类型转换为 mysql 类型

mysql时间戳转换日期格式

sql 时间格式转换

MySql数据库怎样把Long时间与日期相互转换

如何将JAVA DATE类型的日期 转换成指定格式类型的 (如:YYYY-MM-DD) 的 DATE类型数据?

Java中关于时间日期格式保存到mysql的问题