Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

Could you point a MySQL Server query that returns the number of business hours between 02 business dates considering hollydays?

Hi Experts

Could you point a MySQL Server query that returns the number of business hours between 02 business dates  considering hollydays?


Accordingly with a previous reply here:
'FLOOR(DATEDIFF(respondido_em, data) + 1
 - (DATEDIFF(ADDDATE(respondido_em, INTERVAL 1 - DAYOFWEEK(respondido_em) DAY), ADDDATE( data, INTERVAL 1 - DAYOFWEEK(data) DAY)) / 7) * 2
 - (WEEKDAY(data) = 6)
 - (WEEKDAY(respondido_em) = 6)
 - (WEEKDAY(data) = 5)
 - (WEEKDAY(respondido_em) = 5)) * 8 as business_hours',

Open in new window


Considering workable days between 02 dates and 08 workable hours/day.




Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Eduardo Fuerte

ASKER

Hi Tomas!

Perfect solution.

Thank you very much!
I think we've been down this road before, getting the design in PHP.  The answer I've always used is adapted from Practical Application #2 and #9.
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html

https://iconoun.com/demo/temp_eduardo.php
<?php // demo/temp_eduardo.php
/**
 * https://www.experts-exchange.com/questions/28993552/Could-you-point-a-MySQL-Server-query-that-returns-the-number-of-business-hours-between-02-business-dates-considering-hollydays.html
 * https://www.experts-exchange.com/questions/28989520/Could-you-point-a-MySQL-Server-query-that-returns-the-number-of-business-days-between-02-dates.html
 *
 * https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html
 * https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
 */
error_reporting(E_ALL);


// A FUNCTION TO COUNT BUSINESS DAYS BETWEEN GIVEN DATES
function count_business_days($alpha="today", $omega="tomorrow")
{
    // CREATE YOUR ARRAY OF HOLIDAYS, OR LOAD IT FROM AN EXTERNAL TABLE
    $holidays = array();
    $november = strtotime(date('Y') . '-11-0');
    $january  = strtotime(date('Y') . '-01-0');
    $nextyear = mktime(0,0,0, 1, 1, date('Y') + 1);
    $holidays['Dr_M_L_King']  = date('r', strtotime('Third Monday', $january));
    $holidays['Independence'] = date('r', strtotime(date('Y') . '-07-04'));
    $holidays['Thanksgiving'] = date('r', strtotime('Fourth Thursday', $november));
    $holidays['Christmas']    = date('r', strtotime(date('Y') . '-12-25'));
    $holidays['NewYear']      = date('r', $nextyear);

    // ACTIVATE THIS TO SEE THE HOLIDAYS
    // print_r($holidays);

    // INTERPRET THE INPUTS INTO TIMESTAMPS
    if (!$alpha = strtotime($alpha)) return FALSE;
    if (!$omega = strtotime("$omega")) return FALSE;

    // MAKE AN ARRAY OF FUTURE TIMESTAMPS AND RFC2822 DATES
    $arr = range($alpha, $omega, 86400);
    $arr = array_flip($arr);
    foreach ($arr as $timestamp_key => $null)
    {
        // ASSIGN RFC2822 DATE STRINGS TO EACH TIMESTAMP
        $arr[$timestamp_key] = date('r', $timestamp_key);

        // REMOVE THE DAY FROM THE ARRAY IF IT IS A HOLIDAY OR WEEKEND DAY
        if (in_array($arr[$timestamp_key], $holidays)) $arr[$timestamp_key] = 'S';
        if (substr($arr[$timestamp_key],0,1) == 'S') unset($arr[$timestamp_key]);
    }

    // ACTIVATE THIS TO SEE THE ARRAY OF BUSINESS DAYS
    // print_r($arr);

    // RETURN THE COUNT OF BUSINESS DAYS
    return count($arr);
}


// DEMONSTRATE THE FUNCTION
$a = 'Nov 15, 2016';
$z = 'Dec 27, 2016';
$n = count_business_days($a, $z);

// COMPUTE THE HOURS
$h = $n * 8;
echo PHP_EOL . "BETWEEN $a AND $z THERE ARE $n BUSINESS DAYS, THEREFORE $h BUSINESS HOURS";

Open in new window

Hi Ray

Our messages crossed.

The solution need to be MySQL.