0

In my application i want to show a certain date without calculate the weekend days.

So i have date when promotion start: 2016-11-03. promotion lasts for 45 days.

$value['invesdate'] // 2016-11-03

$businessdays= date('Y-m-d', strtotime($value['invesdate'] . ' +45 day'));

Start of promotion 2016-11-03 plus (+) 45 days = 2016-12-15

This is wrong date becouse i calculate and weekend days. The corrct date is 2016-01-05

How do I remove the calculation weekend days ?

Ivan
  • 5,139
  • 11
  • 53
  • 86
  • 1
    Best answer i have got for this http://stackoverflow.com/questions/336127/calculate-business-days – Bik Nov 03 '16 at 05:58

2 Answers2

2
$startDate = '2016-11-03';
$numberofdays = 45;

$d = new DateTime( $startDate );
$t = $d->getTimestamp();

// loop for X days
for($i=0; $i<$numberofdays; $i++){

    // add 1 day to timestamp
    $addDay = 86400;

    // get what day it is next day
    $nextDay = date('w', ($t+$addDay));

    // if it's Saturday or Sunday get $i-1
    if($nextDay == 0 || $nextDay == 6) {
        $i--;
    }

    // modify timestamp, add 1 day
    $t = $t+$addDay;
}

$d->setTimestamp($t);

echo $d->format( 'Y-m-d' ). "\n";

DEMO http://phpio.net/s/azq

Manh Nguyen
  • 930
  • 5
  • 12
-1

Another shortest way to do this:

$businessdays = date('Y-m-d', strtotime($value['invesdate'] . ' +45 weekdays'));
echo $businessdays;

There are many predefined references given in PHP documentation here.

d.coder
  • 1,988
  • 16
  • 23