2

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 e.g:

"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.

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
user3636711
  • 33
  • 1
  • 7
  • Hi, please take a look at this link, it will help you. when you send the date into the function if it is weekends will echo 1, so you can neglect those days. http://stackoverflow.com/questions/4802335/checking-if-date-is-weekend-php?answertab=active#tab-top – logan Sarav Jul 17 '14 at 12:55

3 Answers3

10

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

JJ.
  • 5,425
  • 3
  • 26
  • 31
  • thats works great, what would be the best way to show 4 working days – user3636711 Jul 17 '14 at 13:01
  • so basically order today, delivery date shows 4 working days later (excluding weekends) – user3636711 Jul 17 '14 at 13:02
  • Sorry, missed your follow-up question. I have edited my answer to add four working days vs. only the "next" working day. Run this on Monday and you'll get Friday's date, run it on Tuesday and you'll get the following Monday, etc. – JJ. Jul 18 '14 at 13:33
3

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"));
ashish.negi
  • 502
  • 3
  • 7
  • Thank you so much!! works perfect. just to confirm if i change the following below will also update the number of delivery days? i.e 2 days or 3 days etc case 6: // sat $days = 5; <- change this break; – user3636711 Jul 17 '14 at 13:14
  • yes it works great at the moment, what section would i change in order to change the number of days, so let say for example instead of 4 days we want 3 days. – user3636711 Jul 17 '14 at 13:36
1

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

Lais Gomes
  • 59
  • 4