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