mysql時間相減獲取秒值

2021-10-03 12:04:07 字數 1507 閱讀 6189

mysql時間相減獲取秒值

原創公子清羽 最後發布於2018-06-26 16:13:41 閱讀數 15017  收藏

展開一、mysql時間戳轉換

select unix_timestamp();  獲取當前時間的時間戳

select from_unixtime(1529999834); 時間戳轉換為北京時間

要得到正確的時間相減秒值,有以下3種方法:

1、time_to_sec(timediff(t2, t1)),

2、timestampdiff(second, t1, t2),

3、unix_timestamp(t2) -unix_timestamp(t1)

select accepttime,createtime,    (accepttime-createtime) sec from `req_dlr` where sellorg="cc經銷商88" and createtime>="1527350400" and createtime <= "1529942399" 

and accepttime <>'0' and makeid = '36000000' and reqstate <> '97';

此時用第3種方法,時間戳直接相減

3、timediff(time1,time2),兩個時間相減,返回time差值,如圖

注意:timediff(time1,time2) 函式的兩個引數型別必須相同。

3.1. 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'

3.2、

1、req_dlr表中accepttime,createtime兩個時間戳轉換為北京時間,然後只取時間

2、把取到的時間轉換為秒,相減後求和

select

sum(time_to_sec(m.a)- time_to_sec(m.c))

from

(select

date_format(

from_unixtime(accepttime),

'%h:%i:%s'

) a,

date_format(

from_unixtime(createtime),

'%h:%i:%s'

) cfrom

`req_dlr`

where

createtime >= "1527350400"

and createtime <= "1529942399"

and accepttime <> '0'

and makeid = '36000000'

and reqstate <> '97'

) as m

where

m.a <= '19:00:00'

and m.a >= '09:00:00';

mySql時間相減的問題

剛寫了個功能,其中的一部分包括統計符合條件訂單的arrive time和receiving time的差值 想都沒多想 就直接 arrive time receiving time,結果超級離譜。笑哭 如下 看一下兩個時間差值 其實也就12分鐘,這裡的5255也不知道是什麼數值,秒也不是,毫秒也不對...

獲取日期,實時顯示當前時間,時間相減

var date new date format yyyy mm dd 1 function getnowformatdate 17if day 0 day 9 20if hour 0 hour 9 23if minutes 0 minutes 9 26if seconds 0 seconds 9 ...

資料庫時間相減 mysql時間日期相加相減實現

分享篇mysql中日期的一些操作,就是我們常常會用到的mysql時間日期的相加或者相減的了,這個mysql也自己帶了函式,有需要的朋友可以參考一下。最簡單的方法 select to days str to date 12 1 2001 12 00 00 am m d y to days str to...