Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save iamjuniorpeter/28fbae7881e684a9b67b7da5a49c2591 to your computer and use it in GitHub Desktop.

Select an option

Save iamjuniorpeter/28fbae7881e684a9b67b7da5a49c2591 to your computer and use it in GitHub Desktop.

Revisions

  1. @massiws massiws renamed this gist Apr 12, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @massiws massiws renamed this gist Mar 17, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1 → gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    <?php

    /**
    * Count the number of working days between two dates.
    *
  3. @massiws massiws created this gist Mar 17, 2014.
    55 changes: 55 additions & 0 deletions gistfile1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    /**
    * Count the number of working days between two dates.
    *
    * This function calculate the number of working days between two given dates,
    * taking account of the Public festivities, Easter and Easter Morning days,
    * the day of the Patron Saint (if any) and the working Saturday.
    *
    * @param string $date1 Start date ('YYYY-MM-DD' format)
    * @param string $date2 Ending date ('YYYY-MM-DD' format)
    * @param boolean $workSat TRUE if Saturday is a working day
    * @param string $patron Day of the Patron Saint ('MM-DD' format)
    * @return integer Number of working days ('zero' on error)
    *
    * @author Massimo Simonini <massiws@gmail.com>
    */
    function getWorkdays($date1, $date2, $workSat = FALSE, $patron = NULL) {
    if (!defined('SATURDAY')) define('SATURDAY', 6);
    if (!defined('SUNDAY')) define('SUNDAY', 0);

    // Array of all public festivities
    $publicHolidays = array('01-01', '01-06', '04-25', '05-01', '06-02', '08-15', '11-01', '12-08', '12-25', '12-26');
    // The Patron day (if any) is added to public festivities
    if ($patron) {
    $publicHolidays[] = $patron;
    }

    /*
    * Array of all Easter Mondays in the given interval
    */
    $yearStart = date('Y', strtotime($date1));
    $yearEnd = date('Y', strtotime($date2));

    for ($i = $yearStart; $i <= $yearEnd; $i++) {
    $easter = date('Y-m-d', easter_date($i));
    list($y, $m, $g) = explode("-", $easter);
    $monday = mktime(0,0,0, date($m), date($g)+1, date($y));
    $easterMondays[] = $monday;
    }

    $start = strtotime($date1);
    $end = strtotime($date2);
    $workdays = 0;
    for ($i = $start; $i <= $end; $i = strtotime("+1 day", $i)) {
    $day = date("w", $i); // 0=sun, 1=mon, ..., 6=sat
    $mmgg = date('m-d', $i);
    if ($day != SUNDAY &&
    !in_array($mmgg, $publicHolidays) &&
    !in_array($i, $easterMondays) &&
    !($day == SATURDAY && $workSat == FALSE)) {
    $workdays++;
    }
    }

    return intval($workdays);
    }