How many days have occurred during a given Timespan

advertisements

If we have date range such As:

1st October 2009 - 20th October 2009

Can we calculate how many Mondays have occured during

The answer to this would be (3)

  M   T   W   T   F   S   S
             *1   2   3   4
  5   6   7   8   9  10  11
 12  13  14  15  16  17  18
 19 *20  21  22  23  24  25
 26  27  28  29  30  31

Can we do this in MySQL, if not can it be done in PHP or would i have to create a function


sdt = start date of interval, edt = end date of interval. Change '2' to reflect the day of the week you want to count.

CREATE FUNCTION NMONDAYS( sdt DATETIME,  edt DATETIME )
RETURNS INT DETERMINISTIC RETURN TIMESTAMPDIFF( WEEK, sdt, edt )
+ IF( DAYOFWEEK( edt ) >= 2 AND DAYOFWEEK( edt ) < DAYOFWEEK( sdt ),1,0)