Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get $date + 5 to exclude weekends?

Tags:

date

php

I have some pages that require a date to be set in the future (either 2 or 5 days as set by a variable). This date needs to be only counting Monday - Friday, excluding weekends.

What I have so far ($tts is the variable of 2 or 5 depending on page);

$Today = date('N:m:y');
$NewDate = date('l \t\h\e jS \o\f F',strtotime($Today) + (24*3600*$tts));
$businessDays = [1, 2, 3, 4, 5]; 
echo $NewDate;

This works without excluding weekend days. I have tried to use the $businessDays but I'm unsure of how I can use this to count what days are between $Today and $NewDate

like image 887
nez-bol Avatar asked Mar 11 '23 14:03

nez-bol


2 Answers

Try:

$plusFive = strtotime( '+5 weekday' );
$plusFive = date( 'Y-m-d', $plusFive );

strtotime also takes a second parameter which could be the base for the +5.

like image 95
Merioles Avatar answered Mar 13 '23 02:03

Merioles


Try this code.
It should pick a weekday $tts days ahead.

$Today = date('N:m:y');
$NewDate = date('l \t\h\e jS \o\f F',strtotime($Today . '+' . $tts .' weekdays'));
echo $NewDate;

https://3v4l.org/4SX0m

like image 39
Andreas Avatar answered Mar 13 '23 03:03

Andreas