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 手冊。

 

%W 星期名字(Sunday……Saturday)  
%D 有英語前綴的月份的日期(1st, 2nd, 3rd, 等等。)  
%Y 年, 數字, 4 位  
%y 年, 數字, 2 位  
%a 縮寫的星期名字(Sun……Sat)  
%d 月份中的天數, 數字(00……31)  
%e 月份中的天數, 數字(0……31)  
%m 月, 數字(01……12)  
%c 月, 數字(1……12)  
%b 縮寫的月份名字(Jan……Dec)  
%j 一年中的天數(001……366)  
%H 小時(00……23)  
%k 小時(0……23)  
%h 小時(01……12)  
%I 小時(01……12)  
%l 小時(1……12)  
%i 分鍾, 數字(00……59)  
%r 時間,12 小時(hh:mm:ss [AP]M)  
%T 時間,24 小時(hh:mm:ss)  
%S 秒(00……59)  
%s 秒(00……59)  
%p AM或PM  
%w 一個星期中的天數(0=Sunday ……6=Saturday )  
%U 星期(0……52), 這里星期天是星期的第一天  
%u 星期(0……52), 這里星期一是星期的第一天  
%% 一個文字“%”。  

MySQL (日期、天數)轉換函數:to_days(date), from_days(days)

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

 

FROM_DAYS(N)  給出一個天數N,返回一個DATE值。  
mysql> select FROM_DAYS(729669);  
-> '1997-10-07'  

  

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'

 

查詢數據庫的日期轉換:

to_char(t.kssycdsj,'yyyy-mm-dd hh24:mi')
to_date(#beginTime#,'yyyy-mm-dd hh24:mi')

 

 

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'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM