简体   繁体   中英

PHP date excluding weekend

I am currently doing a website which offers delivery Monday through Friday only.

I am trying to figure out how to add delivery date. For eg:

"Order today and you can expect delivery on DD/MM/YY"

The above date would require to exclude any weekends

So basically if ordered today the delivery day is 4 working days later excluding weekends.

Try this:

<?php
    echo strftime("%e/%m/%y", strtotime("+4 weekday"))
?>

It creates a time string "4 weekdays from now" formatted as DD/MM/YY.

The relative formats for dates that can be used with strtotime are explained here: http://php.net/manual/en/datetime.formats.relative.php

Try this logic. You can modify it according to your needs.

    $curdate = '2014-07-19';
    $mydate=getdate(strtotime($curdate));
    switch($mydate['wday']){
        case 0: // sun
        case 1: // mon
            $days = 4;
            break;
        case 2:    
        case 3:
        case 4:
        case 5:
            $days = 6;
            break;
        case 6: // sat
            $days = 5;
            break;
    }
    echo date('Y-m-d', strtotime("$curdate +$days days"));

Here did exactly what you want

$workdays = array();
$type = CAL_GREGORIAN;
$month = date('n'); // Month ID, 1 through to 12.
$year = date('Y'); // Year in 4 digit 2009 format.
$day_count = cal_days_in_month($type, $month, $year); // Get the amount of days


        for ($i = 1; $i <= $day_count; $i++) {

            $date = $year.'/'.$month.'/'.$i; //format date
            $get_name = date('l', strtotime($date)); //get week day
            $day_name = substr($get_name, 0, 3); // Trim day name to 3 chars

            //if not a weekend add day to array
            if($day_name != 'Sun' && $day_name != 'Sat'){
                $workdays[] = $i;
            }

        }

https://daveismyname.com/show-working-days-of-a-month-excluding-weekends-with-php-bp

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