Add X Days To Date, Excluding Weekends

前端 未结 4 1255
渐次进展
渐次进展 2020-12-17 07:01

I\'m currently developing an online subscription application. I\'m having some challenges on the part where the user will select the Number of Days to subscribe and then the

4条回答
  •  孤城傲影
    2020-12-17 07:01

    I have developed a new function today that can help people who have this type of problem. Depends how the startdate is sent, but assuming you are using Y-m-d you can use DateTime

    getTimestamp();
        // loop for X days
        for( $i = 0; $i < ( $orderDays - 1 ); $i++ ) {
            // get what day it is next day
            $nextDay = date('w', strtotime('+1day', $date_timestamp) );
            // if it's Sunday or Saturday get $i-1
            if( $nextDay == 0 || ( $nextDay == 6 && $saturday_off ) ) { $i--; }
            // modify timestamp, add 1 day
            $date_timestamp = strtotime('+1day', $date_timestamp);
        }
    
        $formatted_date->setTimestamp($date_timestamp);
    
        return $formatted_date->format( 'Y-m-d' );
    }
    
    $orderEndDate = getOrderEndDate( '2020-06-17', 'meal_monthly_6' ); ?>
    

提交回复
热议问题