2011-01-21 132 views
112

兩個日期之間的差異如何計算差值兩個日期之間,格式爲YYYY-MM-DD hh: mm: ss,並在數秒或毫秒的結果值?在MySQL

+11

@didxga:當心:(結束 - 開始)沒有返回的日期時間值之間的差異秒。它返回的數字是十進制數字之間的差異,看起來像yyyymmddhhmmss。 – helloPiers 2015-04-18 08:15:18

回答

215
SELECT TIMEDIFF('2007-12-31 10:02:00','2007-12-30 12:01:01'); 
-- result: 22:00:59. 


SELECT TIMESTAMPDIFF(SECOND,'2007-12-30 12:01:01','2007-12-31 10:02:00'); 
-- result: 79259 the difference in seconds with the time. 
+5

不錯!TIMESTAMPDIFF完美運作!謝謝! – GeoGo 2011-01-21 16:16:39

+1

「幾秒鐘內的差異」是什麼意思?我不明白爲什麼用'24 * 60 * 60'乘以'TIMEDIFF'的結果不等於'TIMESTAMPDIFF`的結果。 – 2013-10-22 09:12:26

+0

此解決方案適合我!但就我而言,我想在DAY中執行TIMESTAMPDIFF,但不考慮週末(sat/sun)。我的意思是,只有一週的時間不同......這是否有可能以一種簡單的方式?如果沒有,我爲此帶來的不便表示歉意,然後我會尋找其他解決方案。 TKS。 – Massa 2015-03-09 13:02:37

32

如果您正在使用DATE列(或可以將它們轉換爲日期列),請嘗試DATEDIFF(),然後乘以24小時,60分鐘,60秒(因爲DATEDIFF以天爲單位返回差異)。從MySQL:

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

例如:

mysql> SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30 00:00:00') * 24*60*60 
+1

我不認爲這項工作。 `DATEDIFF`不返回分數。 – 2016-08-18 15:58:18

0

或者,你可以使用TIMEDIFF功能

mysql> SELECT TIMEDIFF('2000:01:01 00:00:00', '2000:01:01 00:00:00.000001'); 
'-00:00:00.000001' 
mysql> SELECT TIMEDIFF('2008-12-31 23:59:59.000001' , '2008-12-30 01:01:01.000002'); 
'46:58:57.999999' 
+0

TC希望以秒或毫秒爲單位獲得結果值。 – 2011-01-21 15:41:08

4
select 
unix_timestamp('2007-12-30 00:00:00') - 
unix_timestamp('2007-11-30 00:00:00'); 
0

該函數有兩個日期之間的差並顯示它在一個日期格式YYYY-MM-DD。所有你需要的是下面執行的代碼,然後使用該功能。執行後,您可以使用它像這樣

SELECT datedifference(date1, date2) 
FROM .... 
. 
. 
. 
. 


DELIMITER $$ 

CREATE FUNCTION datedifference(date1 DATE, date2 DATE) RETURNS DATE 
NO SQL 

BEGIN 
    DECLARE dif DATE; 
    IF DATEDIFF(date1, DATE(CONCAT(YEAR(date1),'-', MONTH(date1), '-', DAY(date2)))) < 0 THEN 
       SET dif=DATE_FORMAT(
             CONCAT(
              PERIOD_DIFF(date_format(date1, '%y%m'),date_format(date2, '%y%m'))DIV 12 , 
              '-', 
              PERIOD_DIFF(date_format(date1, '%y%m'),date_format(date2, '%y%m'))% 12 , 
              '-', 
              DATEDIFF(date1, DATE(CONCAT(YEAR(date1),'-', MONTH(DATE_SUB(date1, INTERVAL 1 MONTH)), '-', DAY(date2))))), 
             '%Y-%m-%d'); 
    ELSEIF DATEDIFF(date1, DATE(CONCAT(YEAR(date1),'-', MONTH(date1), '-', DAY(date2)))) < DAY(LAST_DAY(DATE_SUB(date1, INTERVAL 1 MONTH))) THEN 
       SET dif=DATE_FORMAT(
             CONCAT(
              PERIOD_DIFF(date_format(date1, '%y%m'),date_format(date2, '%y%m'))DIV 12 , 
              '-', 
              PERIOD_DIFF(date_format(date1, '%y%m'),date_format(date2, '%y%m'))% 12 , 
              '-', 
              DATEDIFF(date1, DATE(CONCAT(YEAR(date1),'-', MONTH(date1), '-', DAY(date2))))), 
             '%Y-%m-%d'); 
    ELSE 
       SET dif=DATE_FORMAT(
             CONCAT(
              PERIOD_DIFF(date_format(date1, '%y%m'),date_format(date2, '%y%m'))DIV 12 , 
              '-', 
              PERIOD_DIFF(date_format(date1, '%y%m'),date_format(date2, '%y%m'))% 12 , 
              '-', 
              DATEDIFF(date1, DATE(CONCAT(YEAR(date1),'-', MONTH(date1), '-', DAY(date2))))), 
             '%Y-%m-%d'); 
    END IF; 

RETURN dif; 
END $$ 
DELIMITER; 
0
select TO_CHAR(TRUNC(SYSDATE)+(to_date('31-MAY-2012 12:25', 'DD-MON-YYYY HH24:MI') 
          - to_date('31-MAY-2012 10:37', 'DD-MON-YYYY HH24:MI')), 
     'HH24:MI:SS') from dual 

- 結果:1點48分00秒

確定它並不完全是什麼OP問,但它是我想要的做:-)

7
SELECT TIMESTAMPDIFF(HOUR,NOW(),'2013-05-15 10:23:23') 
    calculates difference in hour.(for days--> you have to define day replacing hour 
SELECT DATEDIFF('2012-2-2','2012-2-1') 

SELECT TO_DAYS ('2012-2-2')-TO_DAYS('2012-2-1') 
-1

只需將做到這一點:

SELECT (end_time - start_time) FROM t; -- return in Millisecond 
SELECT (end_time - start_time)/1000 FROM t; -- return in Second 
0

在年月日格式兩個日期之間的這段代碼計算的差異。

declare @StartDate datetime 
declare @EndDate datetime 

declare @years int 
declare @months int 
declare @days int 

--NOTE: date of birth must be smaller than As on date, 
--else it could produce wrong results 
set @StartDate = '2013-12-30' --birthdate 
set @EndDate = Getdate()   --current datetime 

--calculate years 
select @years = datediff(year,@StartDate,@EndDate) 

--calculate months if it's value is negative then it 
--indicates after __ months; __ years will be complete 
--To resolve this, we have taken a flag @MonthOverflow... 
declare @monthOverflow int 
select @monthOverflow = case when datediff(month,@StartDate,@EndDate) - 
    (datediff(year,@StartDate,@EndDate) * 12) <0 then -1 else 1 end 
--decrease year by 1 if months are Overflowed 
select @Years = case when @monthOverflow < 0 then @years-1 else @years end 
select @months = datediff(month,@StartDate,@EndDate) - (@years * 12) 

--as we do for month overflow criteria for days and hours 
--& minutes logic will followed same way 
declare @LastdayOfMonth int 
select @LastdayOfMonth = datepart(d,DATEADD 
    (s,-1,DATEADD(mm, DATEDIFF(m,0,@EndDate)+1,0))) 

select @days = case when @monthOverflow<0 and 
    DAY(@StartDate)> DAY(@EndDate) 
then @LastdayOfMonth + 
    (datepart(d,@EndDate) - datepart(d,@StartDate)) - 1 
     else datepart(d,@EndDate) - datepart(d,@StartDate) end 


select 
@Months=case when @days < 0 or DAY(@StartDate)> DAY(@EndDate) then @Months-1 else @Months end 

Declare @lastdayAsOnDate int; 
set @lastdayAsOnDate = datepart(d,DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@EndDate),0))); 
Declare @lastdayBirthdate int; 
set @lastdayBirthdate = datepart(d,DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@StartDate)+1,0))); 

if (@Days < 0) 
(
    select @Days = case when(@lastdayBirthdate > @lastdayAsOnDate) then 
     @lastdayBirthdate + @Days 
    else 
     @lastdayAsOnDate + @Days 
    end 
) 
print convert(varchar,@years) + ' year(s), ' + 
     convert(varchar,@months) + ' month(s), ' + 
     convert(varchar,@days) + ' day(s) ' 
0

爲什麼不

SELECT SUM(日期1 - 日期2)從表

日期1和date2是日期時間

0

如果你存儲在文本字段中日期字符串即可執行此代碼它將獲取過去一天,一個月或一年的天數列表:

SELECT * FROM `table` WHERE STR_TO_DATE(mydate, '%d/%m/%Y') < CURDATE() - INTERVAL 30 DAY AND STR_TO_DATE(date, '%d/%m/%Y') > CURDATE() - INTERVAL 60 DAY 

//This is for a month 

SELECT * FROM `table` WHERE STR_TO_DATE(mydate, '%d/%m/%Y') < CURDATE() - INTERVAL 7 DAY AND STR_TO_DATE(date, '%d/%m/%Y') > CURDATE() - INTERVAL 14 DAY 

//This is for a week 

%d%m%Y是你的日期格式

這個查詢顯示你設置的日期之間的記錄,例如:最近7天以下和最近14天以上的記錄,所以它將是你上週的記錄顯示同樣的概念是針對月份或年份。無論價值你提供在以下日期,如:從7天的下方,另一價值在於它的雙14天。我們在這裏說的是從過去14天以及過去7天以後的所有記錄。這是一個星期的記錄,您可以將價值更改爲一個月30-60天,也可以更改一年。

謝謝你希望它能幫助別人。

0
SELECT TIMESTAMPDIFF(SECOND,'2018-01-19 14:17:15','2018-01-20 14:17:15'); 

第二條本辦法

SELECT (DATEDIFF('1993-02-20','1993-02-19')*(24*60*60))AS 'seccond';

CURRENT_TIME() --this will return current Date 
DATEDIFF('','') --this function will return DAYS and in 1 day there are 24hh 60mm 60sec