Eduardo Fuerte
asked on
Could you point a MySQL Server query that returns the number of business days between 02 dates ?
Hi Experts
Could you point a MySQL Server query that returns the number of business days (day-of-week days - Assuming Saturday and Sunday are not business days) between 02 dates ?
Obs.
I found this query elsewhere (based on a strange algorithm):
That doesn't returns the correct number of days between 02 dates.
Thanks in advance!
Could you point a MySQL Server query that returns the number of business days (day-of-week days - Assuming Saturday and Sunday are not business days) between 02 dates ?
Obs.
I found this query elsewhere (based on a strange algorithm):
SET @E = '20161214';
SET @S = '20161201';
SELECT (DATEDIFF(@E, @S) DIV 7) + MID('0123444401233334012222340111123400012345001234550', 7 * WEEKDAY(@S) + WEEKDAY(@E) + 1, 1) as dias_uteis
That doesn't returns the correct number of days between 02 dates.
Thanks in advance!
This is best handled in the database, IMO, and not PHP. For one, the implementation is much simpler, and you don't have to worry about explicit string to date conversions.
If you had a table with holidays, you can count the number of holidays between the start and end date, and subtract those out, too.
P.S. Weekday numbers, at least in MS SQL, can differ depending on configuration. If that's also the case in MySQL, you'll want to verify that 5 and 6 are Saturday and Sunday.
SELECT (DATEDIFF('2016-12-31', '2016-12-01')) -
((WEEK('2016-12-31') - WEEK('2016-12-01')) * 2) -
(case when weekday('2016-12-31') = 6 then 1 else 0 end) -
(case when weekday('2016-12-01') = 5 then 1 else 0 end);
If you had a table with holidays, you can count the number of holidays between the start and end date, and subtract those out, too.
P.S. Weekday numbers, at least in MS SQL, can differ depending on configuration. If that's also the case in MySQL, you'll want to verify that 5 and 6 are Saturday and Sunday.
ASKER
Hi @Megan
Just one doubt. Didn't the weekday must to be tested to 5 or 6?
Just one doubt. Didn't the weekday must to be tested to 5 or 6?
SELECT (DATEDIFF('2016-12-31', '2016-12-01')) -
((WEEK('2016-12-31') - WEEK('2016-12-01')) * 2) -
(case when (weekday('2016-12-31') = 6 OR weekday('2016-12-31'))= 5 then 1 else 0 end) -
(case when (weekday('2016-12-01') = 6 OR weekday('2016-12-31'))= 5 then 1 else 0 end) AS dias_uteis;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi @zephyr_hex
After some tests your last code looks OK... So I'm planning to put it under production.
Any other final note?
After some tests your last code looks OK... So I'm planning to put it under production.
Any other final note?
Nope. I just suggest you test edge cases. I did preliminary testing with May, October and December, which are all edge scenarios. I didn't look at leap years, month cross overs (i.e. from one month to the next), etc.
ASKER
Thank you !
It's a very good starting point, some other changes if needed will be implemented later, after more apurated tests.
It's a very good starting point, some other changes if needed will be implemented later, after more apurated tests.
Procedural
https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html
Object-Oriented
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
Here's an adaptation:
https://iconoun.com/demo/temp_eduardo.php
Open in new window
You probably want to load the holidays from an external data source that can be checked against official calendars. The reason goes to the rules for "observed" holidays. This year, Christmas is on Sunday, so Dec 25 was already a holiday. The observed holiday for Christmas is Dec 26 in 2016.