简体   繁体   中英

Showing a certain date without calculating the weekend days

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('Ym-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 ?

$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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM