Add X Days to Date, Excluding Weekends

Add X Days To Date, Excluding Weekends

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

?>

Add X days to a Received Date but Exclude Weekends/Holidays from a Date Table

Here is a solution without the time logic.

with tblCalendar(DATE, DAYOFWK, DAY, HOLIDAY) as (values
(date('2019-01-19'), 7, 'Saturday', '')
, (date('2019-01-20'), 1, 'Sunday', '')
, (date('2019-01-21'), 2, 'Monday', 'YES')
, (date('2019-01-22'), 3, 'Tuesday', '')
, (date('2019-01-23'), 4, 'Wednesday', 'YES')
, (date('2019-01-24'), 5, 'Thursday', '')
, (date('2019-01-25'), 6, 'Friday', '')
, (date('2019-01-26'), 7, 'Saturday', '')
)
, mytab (RECEIVEDDATE, DAYS2ADD) as (values
(date('2019-01-19'), 2)
, (date('2019-01-20'), 2)
, (date('2019-01-21'), 2)
, (date('2019-01-22'), 2)
)
select m.*, t.date as DUEDATE
--, dayofweek(date) as DAYOFWK, dayname(date) as DAY
from mytab m
, table
(
select date
from table
(
select
date
, sum(case when HOLIDAY='YES' or dayofweek(date) in (7,1) then 0 else 1 end) over (order by date) as dn_
from tblCalendar t
where t.date > m.RECEIVEDDATE
)
where dn_ = m.DAYS2ADD
fetch first 1 row only
) t;

The idea is to enumerate each day of the calendar after the RECEIVEDDATE (1-st parameter) starting from 1 with the following logic: the number of each day increases by 1 if it's non-holiday non-weekend day (the sum(...) over(...) expression).

Finally, we select a date with the corresponding number of days needed to add (2-nd parameter).

Adding Days to a Date but Excluding Weekends

using Fluent DateTime https://github.com/FluentDateTime/FluentDateTime

var dateTime = DateTime.Now.AddBusinessDays(4);

Add no. of days in a date to get next date(excluding weekends)

Try this

var startDate = "9-DEC-2011";
startDate = new Date(startDate.replace(/-/g, "/"));
var endDate = "", noOfDaysToAdd = 13, count = 0;
while(count < noOfDaysToAdd){
endDate = new Date(startDate.setDate(startDate.getDate() + 1));
if(endDate.getDay() != 0 && endDate.getDay() != 6){
//Date.getDay() gives weekday starting from 0(Sunday) to 6(Saturday)
count++;
}
}
alert(endDate);//You can format this date as per your requirement

Working Demo

Adding x amount of days to a start date but excluding holidays?

You want WORKDAY.INTL()

=WORKDAY.INTL("1/1/2020",75,"0000000","1/18/2020")

the holidays can also be a range:

=WORKDAY.INTL("1/1/2020",75,"0000000",B1:B20)

Php add 5 working days to current date excluding weekends (sat-sun) and excluding (multiple) holidays

You can use the "while statement", looping until get enough 5 days. Each time looping get & check one next day is in the holiday list or not.

Here is the the example:

$holidayDates = array(
'2016-03-26',
'2016-03-27',
'2016-03-28',
'2016-03-29',
'2016-04-05',
);

$count5WD = 0;
$temp = strtotime("2016-03-25 00:00:00"); //example as today is 2016-03-25
while($count5WD<5){
$next1WD = strtotime('+1 weekday', $temp);
$next1WDDate = date('Y-m-d', $next1WD);
if(!in_array($next1WDDate, $holidayDates)){
$count5WD++;
}
$temp = $next1WD;
}

$next5WD = date("Y-m-d", $temp);

echo $next5WD; //if today is 2016-03-25 then it will return 2016-04-06 as many days between are holidays

Add days after date is selected excluding weekends and holidays

Solved it! This answer here helped tremendously.

Code:

function AddBusinessDays(weekDaysToAdd) {
var curdate = new Date();
var realDaysToAdd = 0;
while (weekDaysToAdd > 0){
curdate.setDate(curdate.getDate()+1);
realDaysToAdd++;
//check if current day is business day
if (noWeekendsOrHolidays(curdate)[0]) {
weekDaysToAdd--;
}
}
return realDaysToAdd;

}

var date_billed = $('#datebilled').datepicker('getDate');
var date_overdue = new Date();
var weekDays = AddBusinessDays(30);
date_overdue.setDate(date_billed.getDate() + weekDays);
date_overdue = $.datepicker.formatDate('mm/dd/yy', date_overdue);
$('#datepd').val(date_overdue).prop('readonly', true);

How to get $date + 5 to exclude weekends?

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.



Related Topics



Leave a reply



Submit