0

I have a code where it just simply adds 5 days from the current date. How can I make it skip Saturday and Sunday?

date_default_timezone_set('Asia/Manila');
$current_date = date('Y-m-d');
$hearing = date('Y-m-d', strtotime("$current_date +5 days"));

It's January 27, Wednesday. Adding 5 days to this skipping the weekends would yield the answer February 3, Wednesday. How can I do that? Thank you for your help.

Isabella
  • 455
  • 1
  • 10
  • 23
  • 1
    assuming $current_date will always be a weekday, it follows that you can always add 7 days, unless today is monday (add 5 instead). – Scott Jan 26 '16 at 16:50
  • 1
    hi, @Scott.. i have used your idea. thank you!!! – Isabella Jan 26 '16 at 17:09
  • You should use [Carbon](http://carbon.nesbot.com/) library for this. It makes it easier to calculate dates. You can use `Carbon::now()->isWeekend()` to find weekend days. – Gergely Havlicsek Jan 26 '16 at 18:15
  • Possible duplicate of [PHP date excluding weekend](http://stackoverflow.com/questions/24803897/php-date-excluding-weekend) – styks May 11 '17 at 21:04

2 Answers2

0

You could pass $hearing to isWeekend() until it returns false (because you know that is a weekday)

function isWeekend($date) {
    return (date('N', strtotime($date)) >= 6);
}
crmpicco
  • 16,605
  • 26
  • 134
  • 210
0

You could implement your own Period Iterator. Given a start date and a number of days you want to add, this will add the number of days excluding weekends

Usage

    $startDateTime = new \DateTime('2017-01-27');
    // Will return 5 **business days** in the future.
    // Does not count the current day
    // This particular example will return 02-03-2017
    $endDateTime = addBusinessDays($startDateTime, 5);

The function

function addBusinessDays(\DateTime $startDateTime, $daysToAdd)
{
    $endDateTime = clone $startDateTime;
    $endDateTime->add(new \DateInterval('P' . $daysToAdd . 'D'));
    $period = new \DatePeriod(
        $startDateTime, new \DateInterval('P1D'), $endDateTime,
        // Exclude the start day
        \DatePeriod::EXCLUDE_START_DATE
    );

    $periodIterator = new PeriodIterator($period);
    $adjustedEndingDate = clone $startDateTime;
    while($periodIterator->valid()){
        $adjustedEndingDate = $periodIterator->current();
        // If we run into a weekend, extend our days
        if($periodIterator->isWeekend()){
            $periodIterator->extend();
        }
        $periodIterator->next();
    }

    return $adjustedEndingDate;
}

PeriodIterator Class

class PeriodIterator implements \Iterator
{
    private $current;
    private $period = [];

    public function __construct(\DatePeriod $period) {
        $this->period = $period;
        $this->current = $this->period->getStartDate();
        if(!$period->include_start_date){
            $this->next();
        }

        $this->endDate = $this->period->getEndDate();
    }

    public function rewind() {
        $this->current->subtract($this->period->getDateInterval());
    }

    public function current() {
        return clone $this->current;
    }

    public function key() {
        return $this->current->diff($this->period->getStartDate());
    }

    public function next() {
        $this->current->add($this->period->getDateInterval());
    }

    public function valid() {
        return $this->current < $this->endDate;
    }

    public function extend()
    {
        $this->endDate->add($this->period->getDateInterval());
    }

    public function isSaturday()
    {
        return $this->current->format('N') == 6;
    }

    public function isSunday()
    {
        return $this->current->format('N') == 7;
    }

    public function isWeekend()
    {
        return ($this->isSunday() || $this->isSaturday());
    }
}
styks
  • 3,193
  • 1
  • 23
  • 36