Skip to content

Instantly share code, notes, and snippets.

@tamewhale
Created January 11, 2012 10:36
Show Gist options
  • Save tamewhale/1594092 to your computer and use it in GitHub Desktop.
Save tamewhale/1594092 to your computer and use it in GitHub Desktop.

Revisions

  1. tamewhale revised this gist Feb 11, 2013. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions working_days.php
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,18 @@
    <?php
    function get_next_working_day($timestamp, $no_of_days = 1) {
    // accepts a DateTime object which is now by default
    // and returns a DateTime object x working days from now
    // where x defaults to 1

    function get_next_working_day($date = new DateTime, $no_of_days = 1) {

    // add the number of days passed but skip weekends
    for ($i = 0; $i < $no_of_days; $i++) {
    do {
    $timestamp = strtotime('+1 day', $timestamp);
    $date::add(new DateInterval('P1D'));
    }
    while (in_array(date('D', $timestamp), array('Sat', 'Sun'))); // keep adding if day is a Saturday or Sunday
    while (in_array($date->format('D'), array('Sat', 'Sun'))); // keep adding if day is a Saturday or Sunday
    }

    return $timestamp;
    return $date;

    }
  2. tamewhale created this gist Jan 11, 2012.
    14 changes: 14 additions & 0 deletions working_days.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <?php
    function get_next_working_day($timestamp, $no_of_days = 1) {

    // add the number of days passed but skip weekends
    for ($i = 0; $i < $no_of_days; $i++) {
    do {
    $timestamp = strtotime('+1 day', $timestamp);
    }
    while (in_array(date('D', $timestamp), array('Sat', 'Sun'))); // keep adding if day is a Saturday or Sunday
    }

    return $timestamp;

    }