如何找到当月的工作日和周末?

时间:2017-02-09 05:51:50

标签: php mysql

我想知道当月有多少个工作日和周末。使用 PHP MySQL

我试过下面的代码: -

  select 5 * (DATEDIFF('2017-02-28', '2017-02-01') DIV 7) +  
  MID('0123444401233334012222340111123400001234000123440', 8 * WEEKDAY('2017-02-01') + WEEKDAY('2017-02-28') + 1, 1) as workingdays

请给出寻找工作日和周末的解决方案,

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT CASE WHEN WEEKDAY('2017-02-01') < 5 THEN 5 - WEEKDAY('2017-02-01') ELSE 0 END
+ CASE WHEN WEEKDAY('2017-02-28') < 5 THEN WEEKDAY('2017-02-28') + 1 ELSE 5 END 
+
(DATEDIFF('2017-02-28', '2017-02-01') + 1 
- (7 - WEEKDAY('2017-02-01')) 
- (WEEKDAY('2017-02-28') + 1)
) DIV 7 * 5 AS working_days;

假设start_date:2017-02-01和end_date是2017-02-28

答案 1 :(得分:1)

 SELECT  5 * (DATEDIFF('2017-01-31','2017-01-01') DIV 7) + MID('0123444401233334012222340111123400012345001234550', 7 * WEEKDAY('2017-01-01') + WEEKDAY('2017-01-31') + 1, 1)+1 workingDays FROM DUAL;

希望它对你有所帮助。