码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2017-09-16 16:09:47      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:指定   mys   select   开始时间   rom   date   ros   bsp   cond   

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数据类型(四)日期时间的格式转换

标签:指定   mys   select   开始时间   rom   date   ros   bsp   cond   

原文地址:http://www.cnblogs.com/coe2coe/p/7531306.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!