Add X Days To Date, Excluding Weekends

前端 未结 4 1248
渐次进展
渐次进展 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

    <?php  public static function getOrderEndDate( $start_date, $orderDaysCode ){
    
        $saturday_off = false;
        if( $orderDaysCode == 'meal_monthly_6' ) { $orderDays = 24; }
        elseif( $orderDaysCode == 'meal_monthly_5' ) {
            $orderDays = 20;
            $saturday_off = true;
        }elseif( $orderDaysCode == 'meal_weekly' ) {
            $orderDays = 5;
            $saturday_off = true;
        }
        else{ $orderDays = 1; }   // Daily Meal
    
        $formatted_date = new DateTime( $start_date );
    
        $date_timestamp = $formatted_date->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' ); ?>
    
    0 讨论(0)
  • 2020-12-17 07:02

    I have developed a new function today that can help people who have this type of problem.

    function sumDays($days = 0, $format = 'd/m/Y') {
        $incrementing = $days > 0;
        $days         = abs($days);
        $actualDate   = date('Y-m-d');
    
        while ($days > 0) {
            $tsDate    = strtotime($actualDate . ' ' . ($incrementing ? '+' : '-') . ' 1 days');
            $actualDate = date('Y-m-d', $tsDate);
    
            if (date('N', $tsDate) < 6) {
                $days--;
            }
        }
    
        return date($format, strtotime($actualDate));
    }
    
    0 讨论(0)
  • 2020-12-17 07:04

    depends how the startdate is sent, but assuming you are using Y-m-d you can use DateTime

    eg:

    <?php 
    
        $_POST['startdate'] = '2012-08-14';
        $_POST['numberofdays'] = 10;
    
        $d = new DateTime( $_POST['startdate'] );
        $t = $d->getTimestamp();
    
        // loop for X days
        for($i=0; $i<$_POST['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";
    
    ?>
    
    0 讨论(0)
  • 2020-12-17 07:18

    Same result, shorter version:

    function addDays($days,$format="Y-m-d"){
    
        for($i=0;$i<$days;$i++){
            $day = date('N',strtotime("+".($i+1)."day"));
            if($day>5)
                $days++;
        }
        return date($format,strtotime("+$i day"));
    }
    
    0 讨论(0)
提交回复
热议问题