MySQL的时间差函数TIMESTAMPDIFF、DATEDIFF,随记

需要用MySQL计算时间差,使用TIMESTAMPDIFF、DATEDIFF,记录一下实验结果

 

--0

select datediff(now(), now());



--2

select datediff('2015-04-22 23:59:00', '2015-04-20 00:00:00');



--2

select datediff('2015-04-22 00:00:00', '2015-04-20 23:59:00');



--1

select TIMESTAMPDIFF(DAY, '2015-04-20 23:59:00', '2015-04-22 00:00:00');



--2

select TIMESTAMPDIFF(DAY, '2015-04-20 00:00:00', '2015-04-22 00:00:00');



--2

select TIMESTAMPDIFF(DAY, '2015-04-20 00:00:00', '2015-04-22 12:00:00');



--2

select TIMESTAMPDIFF(DAY, '2015-04-20 00:00:00', '2015-04-22 23:59:00');



--71

select TIMESTAMPDIFF(HOUR, '2015-04-20 00:00:00', '2015-04-22 23:00:00');



--4260

select TIMESTAMPDIFF(MINUTE, '2015-04-20 00:00:00', '2015-04-22 23:00:00');
View Code

 

关于TIMESTAMPDIFF描述,见http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timestampdiff

如需知道TIMESTAMPDIFF还能以哪些单位返回数据,可参考TIMESTAMPADD的描述:http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timestampadd

 

你可能感兴趣的:(Timestamp)