我停了下来,我在mysql中做了一个计算小时数的函数,但它一直给我错误的数字,我已经看了好几个小时了,我只是看不出我做错了什么
FUNCTION `WorkingHours`(`stardate` TIMESTAMP, `enddate` TIMESTAMP) RETURNS int(11)
BEGIN
DECLARE result DECIMAL(20,10) DEFAULT 0;
DECLARE TotWeeks DECIMAL(20,10);
DECLARE FullWeeks INT;
DECLARE RestDays DECIMAL(20,10);
DECLARE StartDay INT DEFAULT WEEKDAY(stardate) + 1;
SET TotWeeks = (TIMESTAMPDIFF(HOUR,stardate,enddate))/(24*7);
SET FullWeeks = FLOOR(TotWeeks);
SET RestDays = ROUND((TotWeeks-FullWeeks) * 7);
IF(RestDays + StartDay) > 5 THEN SET result = ROUND((TotWeeks*7*24) - (FullWeeks*2*24 + (((RestDays + StartDay) - 5) * 24)));
ELSE SET result = ROUND((TotWeeks*7*24) - (FullWeeks*2*24));
END IF;
RETURN result;
END
如果有人有任何建议或替代方法,我非常乐意替换这个。
Startdate:2017-07-05 12:17:18
Enddate:2017-07-07 18:30:42
给我 - 5
编辑: 这些日期给出-45
Startdate:2017-07-09 13:55:41
Enddate:2017-07-10 17:31:56
该功能几乎每次都可以正常工作,但有几次没有,我只是不知道为什么