Reputation: 33
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.
Upvotes: 2
Views: 9465
Reputation: 59
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
Upvotes: 1
Reputation: 5475
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
Upvotes: 10
Reputation: 534
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"));
Upvotes: 3