mysql中格式化日期详解


 MySQL 获得当前日期时间 函数

获得当前日期+时间(date + time)函数:now()

复制代码
mysql> select now();

+---------------------+
| now() |
+---------------------+
| 2008-08-08 22:20:46 |
+---------------------+
复制代码

获得当前日期+时间(date + time)函数:sysdate()
sysdate() 日期时间函数跟 now() 类似,不同之处在于:now() 在执行开始时值就得到了, sysdate() 在函数执行时动态得到值。看下面的例子就明白了:

复制代码
mysql> select now(), sleep(3), now();

+---------------------+----------+---------------------+
| now() | sleep(3) | now() |
+---------------------+----------+---------------------+
| 2008-08-08 22:28:21 | 0 | 2008-08-08 22:28:21 |
+---------------------+----------+---------------------+
复制代码

sysdate() 日期时间函数,一般情况下很少用到。

 

MySQL 获得当前时间戳函数:current_timestamp, current_timestamp()

复制代码
mysql> select current_timestamp, current_timestamp();

+---------------------+---------------------+
| current_timestamp | current_timestamp() |
+---------------------+---------------------+
| 2008-08-09 23:22:24 | 2008-08-09 23:22:24 |
+---------------------+---------------------+
复制代码

 

MySQL 日期转换函数、时间转换函数

MySQL Date/Time to Str(日期/时间转换为字符串)函数:date_format(date,format), time_format(time,format)

复制代码
mysql> select date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s');

+----------------------------------------------------+
| date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s') |
+----------------------------------------------------+
| 20080808222301 |
+----------------------------------------------------+
复制代码

MySQL 日期、时间转换函数:date_format(date,format), time_format(time,format) 能够把一个日期/时间转换成各种各样的字符串格式。它是 str_to_date(str,format) 函数的 一个逆转换。

 

MySQL Str to Date (字符串转换为日期)函数:str_to_date(str, format)

select str_to_date('08/09/2008', '%m/%d/%Y'); -- 2008-08-09
select str_to_date('08/09/08' , '%m/%d/%y'); -- 2008-08-09
select str_to_date('08.09.2008', '%m.%d.%Y'); -- 2008-08-09
select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30
select str_to_date('08.09.2008 08:09:30', '%m.%d.%Y %h:%i:%s'); -- 2008-08-09 08:09:30

可以看到,str_to_date(str,format) 转换函数,可以把一些杂乱无章的字符串转换为日期格式。另外,它也可以转换为时间。“format” 可以参看 MySQL 手册。

 

MySQL (日期、天数)转换函数:to_days(date), from_days(days)

select to_days('0000-00-00'); -- 0
select to_days('2008-08-08'); -- 733627

 

MySQL (时间、秒)转换函数:time_to_sec(time), sec_to_time(seconds)

select time_to_sec('01:00:05'); -- 3605
select sec_to_time(3605); -- '01:00:05'

 

MySQL 拼凑日期、时间函数:makdedate(year,dayofyear), maketime(hour,minute,second)

select makedate(2001,31); -- '2001-01-31'
select makedate(2001,32); -- '2001-02-01'
select maketime(12,15,30); -- '12:15:30'

 

MySQL (Unix 时间戳、日期)转换函数

unix_timestamp(),
unix_timestamp(date),
from_unixtime(unix_timestamp),
from_unixtime(unix_timestamp,format)


下面是示例:

复制代码
select unix_timestamp(); -- 1218290027
select unix_timestamp('2008-08-08'); -- 1218124800
select unix_timestamp('2008-08-08 12:30:00'); -- 1218169800

select from_unixtime(1218290027); -- '2008-08-09 21:53:47'
select from_unixtime(1218124800); -- '2008-08-08 00:00:00'
select from_unixtime(1218169800); -- '2008-08-08 12:30:00'

select from_unixtime(1218169800, '%Y %D %M %h:%i:%s %x'); -- '2008 8th August 12:30:00 2008'
复制代码

 

 

MySQL 日期时间计算函数

 

MySQL 为日期增加一个时间间隔:date_add()

复制代码
set @dt = now();

select date_add(@dt, interval 1 day); -- add 1 day
select date_add(@dt, interval 1 hour); -- add 1 hour
select date_add(@dt, interval 1 minute); -- ...
select date_add(@dt, interval 1 second);
select date_add(@dt, interval 1 microsecond);
select date_add(@dt, interval 1 week);
select date_add(@dt, interval 1 month);
select date_add(@dt, interval 1 quarter);
select date_add(@dt, interval 1 year);

select date_add(@dt, interval -1 day); -- sub 1 day
复制代码

 

MySQL adddate(), addtime()函数,可以用 date_add() 来替代。下面是 date_add() 实现 addtime() 功能示例:

复制代码
mysql> set @dt = '2008-08-09 12:12:33';

mysql>
mysql> select date_add(@dt, interval '01:15:30' hour_second);

+------------------------------------------------+
| date_add(@dt, interval '01:15:30' hour_second) |
+------------------------------------------------+
| 2008-08-09 13:28:03 |
+------------------------------------------------+

mysql> select date_add(@dt, interval '1 01:15:30' day_second);

+-------------------------------------------------+
| date_add(@dt, interval '1 01:15:30' day_second) |
+-------------------------------------------------+
| 2008-08-10 13:28:03 |
+-------------------------------------------------+
复制代码

 

MySQL 为日期减去一个时间间隔:date_sub()

复制代码
mysql> select date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second);

+----------------------------------------------------------------+
| date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second) |
+----------------------------------------------------------------+
| 1997-12-30 22:58:59 |
+----------------------------------------------------------------+
复制代码

MySQL date_sub() 日期时间函数 和 date_add() 用法一致,不再赘述。

 

MySQL 日期、时间相减函数:datediff(date1,date2), timediff(time1,time2)

MySQL datediff(date1,date2):两个日期相减 date1 - date2,返回天数。
select datediff('2008-08-08', '2008-08-01'); -- 7
select datediff('2008-08-01', '2008-08-08'); -- -7

MySQL timediff(time1,time2):两个日期相减 time1 - time2,返回 time 差值。

select timediff('2008-08-08 08:08:08', '2008-08-08 00:00:00'); -- 08:08:08
select timediff('08:08:08', '00:00:00'); -- 08:08:08

注意:timediff(time1,time2) 函数的两个参数类型必须相同。

 

MySQL 时间戳(timestamp)转换、增、减函数:

timestamp(date) -- date to timestamp
timestamp(dt,time) -- dt + time
timestampadd(unit,interval,datetime_expr) --
timestampdiff(unit,datetime_expr1,datetime_expr2) --

请看示例部分:

复制代码
select timestamp('2008-08-08'); -- 2008-08-08 00:00:00
select timestamp('2008-08-08 08:00:00', '01:01:01'); -- 2008-08-08 09:01:01
select timestamp('2008-08-08 08:00:00', '10 01:01:01'); -- 2008-08-18 09:01:01

select timestampadd(day, 1, '2008-08-08 08:00:00'); -- 2008-08-09 08:00:00
select date_add('2008-08-08 08:00:00', interval 1 day); -- 2008-08-09 08:00:00

MySQL timestampadd() 函数类似于 date_add()。
select timestampdiff(year,'2002-05-01','2001-01-01'); -- -1
select timestampdiff(day ,'2002-05-01','2001-01-01'); -- -485
select timestampdiff(hour,'2008-08-08 12:00:00','2008-08-08 00:00:00'); -- -12

select datediff('2008-08-08 12:00:00', '2008-08-01 00:00:00'); -- 7
复制代码

MySQL timestampdiff() 函数就比 datediff() 功能强多了,datediff() 只能计算两个日期(date)之间相差的天数。

 

MySQL 时区(timezone)转换函数

convert_tz(dt,from_tz,to_tz)

select convert_tz('2008-08-08 12:00:00', '+08:00', '+00:00'); -- 2008-08-08 04:00:00

时区转换也可以通过 date_add, date_sub, timestampadd 来实现。

select date_add('2008-08-08 12:00:00', interval -8 hour); -- 2008-08-08 04:00:00
select date_sub('2008-08-08 12:00:00', interval 8 hour); -- 2008-08-08 04:00:00
select timestampadd(hour, -8, '2008-08-08 12:00:00'); -- 2008-08-08 04:00:00
 
 
 
1
DATE_FORMAT( date ,format)
 

format参数的格式有

%a 缩写星期名
%b 缩写月名
%c 月,数值
%D 带有英文前缀的月中的天
%d 月的天,数值(00-31)
%e 月的天,数值(0-31)
%f 微秒
%H 小时 (00-23)
%h 小时 (01-12)
%I 小时 (01-12)
%i 分钟,数值(00-59)
%j 年的天 (001-366)
%k 小时 (0-23)
%l 小时 (1-12)
%M 月名
%m 月,数值(00-12)
%p AM 或 PM
%r 时间,12-小时(hh:mm:ss AM 或 PM)
%S 秒(00-59)
%s 秒(00-59)
%T 时间, 24-小时 (hh:mm:ss)
%U 周 (00-53) 星期日是一周的第一天
%u 周 (00-53) 星期一是一周的第一天
%V 周 (01-53) 星期日是一周的第一天,与 %X 使用
%v 周 (01-53) 星期一是一周的第一天,与 %x 使用
%W 星期名
%w 周的天 (0=星期日, 6=星期六)
%X 年,其中的星期日是周的第一天,4 位,与 %V 使用
%x 年,其中的星期一是周的第一天,4 位,与 %v 使用
%Y 年,4 位
%y 年,2 位

例子:

?
1
2
3
4
DATE_FORMAT(NOW(), '%b %d %Y %h:%i %p' )
DATE_FORMAT(NOW(), '%m-%d-%Y' )
DATE_FORMAT(NOW(), '%d %b %y' )
DATE_FORMAT(NOW(), '%d %b %Y %T:%f' )
 

输出结果:

?
1
2
3
4
Dec 29 2008 11:45 PM
12-29-2008
29 Dec 08
29 Dec 2008 16:25:46
 

2. MySQL 格式化函数 FROM_UNIXTIME()

?
1
2
3
SELECT FROM_UNIXTIME( date , '%Y-%c-%d %h:%i:%s' ) as post_date ,
date_format(NOW(), '%Y-%c-%d %h:%i:%s' ) as post_date_gmt
FROM `article` where outkey = 'Y'
 

1、FROM_UNIXTIME( unix_timestamp )

参数:一般为10位的时间戳,如:1417363200

返回值:有两种,可能是类似 'YYYY-MM-DD HH:MM:SS' 这样的字符串,也有可能是类似于 YYYYMMDDHHMMSS.uuuuuu 这样的数字,具体返回什么取决于该函数被调用的形式。

?
1
2
3
4
5
6
7
mysql> select FROM_UNIXTIME(1344887103);
+ ---------------------------+
| FROM_UNIXTIME(1344887103) |
+ ---------------------------+
| 2012-08-14 03:45:03  |
+ ---------------------------+
1 row in set (0.00 sec)
 

2、FROM_UNIXTIME( unix_timestamp ,format )

参数 unix_timestamp :与方法 FROM_UNIXTIME( unix_timestamp ) 中的参数含义一样;

参数 format : 转换之后的时间字符串显示的格式;

返回值:按照指定的时间格式显示的字符串;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mysql> select FROM_UNIXTIME(1344887103, '%Y-%M-%D %h:%i:%s' );
+ -----------------------------------------------+
| FROM_UNIXTIME(1344887103, '%Y-%M-%D %h:%i:%s' ) |
+ -----------------------------------------------+
| 2012-August-14th 03:45:03      |
+ -----------------------------------------------+
1 row in set (0.00 sec)
mysql> select FROM_UNIXTIME(1344887103, '%Y-%m-%D %h:%i:%s' );
+ -----------------------------------------------+
| FROM_UNIXTIME(1344887103, '%Y-%m-%D %h:%i:%s' ) |
+ -----------------------------------------------+
| 2012-08-14th 03:45:03       |
+ -----------------------------------------------+
  
1 row in set (0.00 sec)
 

3、判断是不是同一天:

?
1
2
3
4
5
6
7
8
SELECT tbl_gamedata.GameMapName,tbl_playerdata.GameMode, tbl_gamedata.MatchMode, tbl_playerdata.GameResult, SUM (tbl_playerdata.GameIsWin) AS tday_winCount,
    SUM (tbl_playerdata.AssistCount) AS tday_assistCount, SUM (tbl_playerdata.KillCount) AS tday_killCount,
    SUM (tbl_player_title.ThreeKill) AS tday_threeKill, SUM (tbl_player_title.FourKill) AS tday_fourKill, SUM (tbl_player_title.FiveKill) AS tday_fiveKill
  FROM tbl_playerdata
   LEFT JOIN tbl_gamedata ON tbl_playerdata.GameID = tbl_gamedata.GameID
   LEFT JOIN tbl_player_title ON tbl_player_title.GameID = tbl_playerdata.GameID AND tbl_player_title.PlayerID = tbl_playerdata.PlayerID
  WHERE tbl_playerdata.PlayerID = user_id AND (tbl_playerdata.GameResult = 2 OR tbl_playerdata.GameResult = 3) AND TO_DAYS(FROM_UNIXTIME(tbl_playerdata.GameStartTime)) = TO_DAYS(NOW())
  GROUP BY tbl_gamedata.GameMapName, tbl_playerdata.GameMode,tbl_gamedata.MatchMode,tbl_playerdata.GameResult;
 

其中TO_DAYS(FROM_UNIXTIME(tbl_playerdata.GameStartTime)) = TO_DAYS(NOW()) 就是我们需要的判断

 

<div class="postBody">                <div id="cnblogs_post_body" class="blogpost-body blogpost-body-html"><h2>&nbsp;<strong>MySQL 获得当前日期时间 函数</strong></h2><p>获得当前日期+时间(date + time)函数:now()</p><div><div><a title="复制代码"><img title="MySQL&nbsp;<wbr>获得当前日期时间&nbsp;<wbr>函数" src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></div><pre>mysql&gt; select now();
+---------------------+| now() |+---------------------+| 2008-08-08 22:20:46 |+---------------------+</pre><div><a title="复制代码"><img title="MySQL&nbsp;<wbr>获得当前日期时间&nbsp;<wbr>函数" src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></div></div><p>获得当前日期+时间(date + time)函数:sysdate()<br>sysdate() 日期时间函数跟 now() 类似,不同之处在于:now() 在执行开始时值就得到了, sysdate() 在函数执行时动态得到值。看下面的例子就明白了:</p><div><div><a title="复制代码"><img title="MySQL&nbsp;<wbr>获得当前日期时间&nbsp;<wbr>函数" src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></div><pre>mysql&gt; select now(), sleep(3), now();
+---------------------+----------+---------------------+| now() | sleep(3) | now() |+---------------------+----------+---------------------+| 2008-08-08 22:28:21 | 0 | 2008-08-08 22:28:21 |+---------------------+----------+---------------------+</pre><div><a title="复制代码"><img title="MySQL&nbsp;<wbr>获得当前日期时间&nbsp;<wbr>函数" src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></div></div><p>sysdate() 日期时间函数,一般情况下很少用到。</p><p>&nbsp;</p><p>MySQL 获得当前时间戳函数:current_timestamp, current_timestamp()</p><div><div><a title="复制代码"><img title="MySQL&nbsp;<wbr>获得当前日期时间&nbsp;<wbr>函数" src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></div><pre>mysql&gt; select current_timestamp, current_timestamp();
+---------------------+---------------------+| current_timestamp | current_timestamp() |+---------------------+---------------------+| 2008-08-09 23:22:24 | 2008-08-09 23:22:24 |+---------------------+---------------------+</pre><div><a title="复制代码"><img title="MySQL&nbsp;<wbr>获得当前日期时间&nbsp;<wbr>函数" src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></div></div><h2>&nbsp;</h2><h2><strong>MySQL 日期转换函数、时间转换函数</strong></h2><p>MySQL Date/Time to Str(日期/时间转换为字符串)函数:date_format(date,format), time_format(time,format)</p><div><div><a title="复制代码"><img title="MySQL&nbsp;<wbr>获得当前日期时间&nbsp;<wbr>函数" src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></div><pre>mysql&gt; select date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s');
+----------------------------------------------------+| date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s') |+----------------------------------------------------+| 20080808222301 |+----------------------------------------------------+</pre><div><a title="复制代码"><img title="MySQL&nbsp;<wbr>获得当前日期时间&nbsp;<wbr>函数" src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></div></div><p>MySQL 日期、时间转换函数:date_format(date,format), time_format(time,format) 能够把一个日期/时间转换成各种各样的字符串格式。它是 str_to_date(str,format) 函数的 一个逆转换。</p><p>&nbsp;</p><p>MySQL Str to Date (字符串转换为日期)函数:str_to_date(str, format)</p><div><pre>select str_to_date('08/09/2008', '%m/%d/%Y'); -- 2008-08-09select str_to_date('08/09/08' , '%m/%d/%y'); -- 2008-08-09select str_to_date('08.09.2008', '%m.%d.%Y'); -- 2008-08-09select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30select str_to_date('08.09.2008 08:09:30', '%m.%d.%Y %h:%i:%s'); -- 2008-08-09 08:09:30</pre></div><p>可以看到,str_to_date(str,format) 转换函数,可以把一些杂乱无章的字符串转换为日期格式。另外,它也可以转换为时间。“format” 可以参看 MySQL 手册。</p><p>&nbsp;</p><p>MySQL (日期、天数)转换函数:to_days(date), from_days(days)</p><div><pre>select to_days('0000-00-00'); -- 0select to_days('2008-08-08'); -- 733627</pre></div><p>&nbsp;</p><p>MySQL (时间、秒)转换函数:time_to_sec(time), sec_to_time(seconds)</p><div><pre>select time_to_sec('01:00:05'); -- 3605select sec_to_time(3605); -- '01:00:05'</pre></div><p>&nbsp;</p><p>MySQL 拼凑日期、时间函数:makdedate(year,dayofyear), maketime(hour,minute,second)</p><div><pre>select makedate(2001,31); -- '2001-01-31'select makedate(2001,32); -- '2001-02-01'select maketime(12,15,30); -- '12:15:30'</pre></div><p>&nbsp;</p><p>MySQL (Unix 时间戳、日期)转换函数</p><div><pre>unix_timestamp(),unix_timestamp(date),from_unixtime(unix_timestamp),from_unixtime(unix_timestamp,format)</pre></div><p><br>下面是示例:</p><div><div><a title="复制代码"><img title="MySQL&nbsp;<wbr>获得当前日期时间&nbsp;<wbr>函数" src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></div><pre>select unix_timestamp(); -- 1218290027select unix_timestamp('2008-08-08'); -- 1218124800select unix_timestamp('2008-08-08 12:30:00'); -- 1218169800
select from_unixtime(1218290027); -- '2008-08-09 21:53:47'select from_unixtime(1218124800); -- '2008-08-08 00:00:00'select from_unixtime(1218169800); -- '2008-08-08 12:30:00'
select from_unixtime(1218169800, '%Y %D %M %h:%i:%s %x'); -- '2008 8th August 12:30:00 2008'</pre><div><a title="复制代码"><img title="MySQL&nbsp;<wbr>获得当前日期时间&nbsp;<wbr>函数" src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></div></div><p>&nbsp;</p><p>&nbsp;</p><h2><strong>MySQL 日期时间计算函数</strong></h2><p>&nbsp;</p><p>MySQL 为日期增加一个时间间隔:date_add()</p><div><div><a title="复制代码"><img title="MySQL&nbsp;<wbr>获得当前日期时间&nbsp;<wbr>函数" src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></div><pre>set @dt = now();
select date_add(@dt, interval 1 day); -- add 1 dayselect date_add(@dt, interval 1 hour); -- add 1 hourselect date_add(@dt, interval 1 minute); -- ...select date_add(@dt, interval 1 second);select date_add(@dt, interval 1 microsecond);select date_add(@dt, interval 1 week);select date_add(@dt, interval 1 month);select date_add(@dt, interval 1 quarter);select date_add(@dt, interval 1 year);
select date_add(@dt, interval -1 day); -- sub 1 day</pre><div><a title="复制代码"><img title="MySQL&nbsp;<wbr>获得当前日期时间&nbsp;<wbr>函数" src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></div></div><p>&nbsp;</p><p>MySQL adddate(), addtime()函数,可以用 date_add() 来替代。下面是 date_add() 实现 addtime() 功能示例:</p><div><div><a title="复制代码"><img title="MySQL&nbsp;<wbr>获得当前日期时间&nbsp;<wbr>函数" src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></div><pre>mysql&gt; set @dt = '2008-08-09 12:12:33';
mysql&gt;mysql&gt; select date_add(@dt, interval '01:15:30' hour_second);
+------------------------------------------------+| date_add(@dt, interval '01:15:30' hour_second) |+------------------------------------------------+| 2008-08-09 13:28:03 |+------------------------------------------------+
mysql&gt; select date_add(@dt, interval '1 01:15:30' day_second);
+-------------------------------------------------+| date_add(@dt, interval '1 01:15:30' day_second) |+-------------------------------------------------+| 2008-08-10 13:28:03 |+-------------------------------------------------+</pre><div><a title="复制代码"><img title="MySQL&nbsp;<wbr>获得当前日期时间&nbsp;<wbr>函数" src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></div></div><p>&nbsp;</p><p>MySQL 为日期减去一个时间间隔:date_sub()</p><div><div><a title="复制代码"><img title="MySQL&nbsp;<wbr>获得当前日期时间&nbsp;<wbr>函数" src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></div><pre>mysql&gt; select date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second);
+----------------------------------------------------------------+| date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second) |+----------------------------------------------------------------+| 1997-12-30 22:58:59 |+----------------------------------------------------------------+</pre><div><a title="复制代码"><img title="MySQL&nbsp;<wbr>获得当前日期时间&nbsp;<wbr>函数" src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></div></div><p>MySQL date_sub() 日期时间函数 和 date_add() 用法一致,不再赘述。</p><p>&nbsp;</p><p>MySQL 日期、时间相减函数:datediff(date1,date2), timediff(time1,time2)</p><div><pre>MySQL datediff(date1,date2):两个日期相减 date1 - date2,返回天数。select datediff('2008-08-08', '2008-08-01'); -- 7select datediff('2008-08-01', '2008-08-08'); -- -7</pre></div><p>MySQL timediff(time1,time2):两个日期相减 time1 - time2,返回 time 差值。</p><div><pre>select timediff('2008-08-08 08:08:08', '2008-08-08 00:00:00'); -- 08:08:08select timediff('08:08:08', '00:00:00'); -- 08:08:08</pre></div><p>注意:timediff(time1,time2) 函数的两个参数类型必须相同。</p><p>&nbsp;</p><p>MySQL 时间戳(timestamp)转换、增、减函数:</p><div><pre>timestamp(date) -- date to timestamptimestamp(dt,time) -- dt + timetimestampadd(unit,interval,datetime_expr) --timestampdiff(unit,datetime_expr1,datetime_expr2) --</pre></div><p>请看示例部分:</p><div><div><a title="复制代码"><img title="MySQL&nbsp;<wbr>获得当前日期时间&nbsp;<wbr>函数" src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></div><pre>select timestamp('2008-08-08'); -- 2008-08-08 00:00:00select timestamp('2008-08-08 08:00:00', '01:01:01'); -- 2008-08-08 09:01:01select timestamp('2008-08-08 08:00:00', '10 01:01:01'); -- 2008-08-18 09:01:01
select timestampadd(day, 1, '2008-08-08 08:00:00'); -- 2008-08-09 08:00:00select date_add('2008-08-08 08:00:00', interval 1 day); -- 2008-08-09 08:00:00
MySQL timestampadd() 函数类似于 date_add()。select timestampdiff(year,'2002-05-01','2001-01-01'); -- -1select timestampdiff(day ,'2002-05-01','2001-01-01'); -- -485select timestampdiff(hour,'2008-08-08 12:00:00','2008-08-08 00:00:00'); -- -12
select datediff('2008-08-08 12:00:00', '2008-08-01 00:00:00'); -- 7</pre><div><a title="复制代码"><img title="MySQL&nbsp;<wbr>获得当前日期时间&nbsp;<wbr>函数" src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></div></div><p>MySQL timestampdiff() 函数就比 datediff() 功能强多了,datediff() 只能计算两个日期(date)之间相差的天数。</p><p>&nbsp;</p><h2><strong>MySQL 时区(timezone)转换函数</strong></h2><div><pre>convert_tz(dt,from_tz,to_tz)
select convert_tz('2008-08-08 12:00:00', '+08:00', '+00:00'); -- 2008-08-08 04:00:00</pre></div><p>时区转换也可以通过 date_add, date_sub, timestampadd 来实现。</p><div><pre>select date_add('2008-08-08 12:00:00', interval -8 hour); -- 2008-08-08 04:00:00select date_sub('2008-08-08 12:00:00', interval 8 hour); -- 2008-08-08 04:00:00select timestampadd(hour, -8, '2008-08-08 12:00:00'); -- 2008-08-08 04:00:00</pre></div></div><div id="MySignature"></div><div class="clear"></div><div id="blog_post_info_block"><div id="BlogPostCategory">    分类:             <a href="https://www.cnblogs.com/php12-cn/category/1167110.html" target="_blank">database</a></div>

    <div id="blog_post_info"><div id="green_channel">        <a href="javascript:void(0);" id="green_channel_digg" onclick="DiggIt(8882221,cb_blogId,1);green_channel_success(this,'谢谢推荐!');">好文要顶</a>        <a id="green_channel_follow" onclick="follow('35b745dc-fc1a-4d4b-0e4c-08d4ef52ecb6');" href="javascript:void(0);">关注我</a>    <a id="green_channel_favorite" onclick="AddToWz(cb_entryId);return false;" href="javascript:void(0);">收藏该文</a>    <a id="green_channel_weibo" href="javascript:void(0);" title="分享至新浪微博" onclick="ShareToTsina()"><img src="https://common.cnblogs.com/images/icon_weibo_24.png" alt=""></a>    <a id="green_channel_wechat" href="javascript:void(0);" title="分享至微信" onclick="shareOnWechat()"><img src="https://common.cnblogs.com/images/wechat.png" alt=""></a></div><div id="author_profile">    <div id="author_profile_info" class="author_profile_info">            <a href="https://home.cnblogs.com/u/php12-cn/" target="_blank"><img src="https://pic.cnblogs.com/face/sample_face.gif" class="author_avatar" alt=""></a>        <div id="author_profile_detail" class="author_profile_info">            <a href="https://home.cnblogs.com/u/php12-cn/">狂猫</a><br>            <a href="https://home.cnblogs.com/u/php12-cn/followees/">关注 - 0</a><br>            <a href="https://home.cnblogs.com/u/php12-cn/followers/">粉丝 - 2</a>        </div>    </div>    <div class="clear"></div>    <div id="author_profile_honor"></div>    <div id="author_profile_follow">                <a href="javascript:void(0);" onclick="follow('35b745dc-fc1a-4d4b-0e4c-08d4ef52ecb6');return false;">+加关注</a>    </div></div><div id="div_digg">    <div class="diggit" onclick="votePost(8882221,'Digg')">        <span class="diggnum" id="digg_count">5</span>    </div>    <div class="buryit" onclick="votePost(8882221,'Bury')">        <span class="burynum" id="bury_count">0</span>    </div>    <div class="clear"></div>    <div class="diggword" id="digg_tips">    </div></div>
<script type="text/javascript">    currentDiggType = 0;</script></div>    <div class="clear"></div>    <div id="post_next_prev">
    <a href="https://www.cnblogs.com/php12-cn/p/8734988.html" class="p_n_p_prefix">« </a> 上一篇:    <a href="https://www.cnblogs.com/php12-cn/p/8734988.html" title="发布于 2018-04-07 21:26">php  一段 shmop</a>    <br>    <a href="https://www.cnblogs.com/php12-cn/p/9085217.html" class="p_n_p_prefix">» </a> 下一篇:    <a href="https://www.cnblogs.com/php12-cn/p/9085217.html" title="发布于 2018-05-24 21:32">frp 内网穿透配置</a>
</div></div>            </div>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM