Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next business day of given date in PHP

Tags:

date

php

next

Does anyone have a PHP snippet to calculate the next business day for a given date? How does, for example, YYYY-MM-DD need to be converted to find out the next business day?

Example: For 03.04.2011 (DD-MM-YYYY) the next business day is 04.04.2011. For 08.04.2011 the next business day is 11.04.2011.

This is the variable containing the date I need to know the next business day for

$cubeTime['time']; 

Variable contains: 2011-04-01 result of the snippet should be: 2011-04-04

like image 464
Marko Avatar asked Apr 03 '11 19:04

Marko


People also ask

How can I get next Monday date in PHP?

echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "<br>"); echo(strtotime("next Monday") . "<br>");

How does PHP calculate working days?

php //The function returns the no. of business days between two dates and it skips the holidays function getWorkingDays($startDate,$endDate,$holidays){ // do strtotime calculations just once $endDate = strtotime($endDate); $startDate = strtotime($startDate); //The total number of days between the two dates.

What does NOW () return in PHP?

MySQL function NOW() returns the current timestamp.


2 Answers

Next Weekday

This finds the next weekday from a specific date (not including Saturday or Sunday):

echo date('Y-m-d', strtotime('2011-04-05 +1 Weekday')); 

You could also do it with a date variable of course:

$myDate = '2011-04-05'; echo date('Y-m-d', strtotime($myDate . ' +1 Weekday')); 

UPDATE: Or, if you have access to PHP's DateTime class (very likely):

$date = new DateTime('2018-01-27'); $date->modify('+7 weekday'); echo $date->format('Y-m-d'); 

Want to Skip Holidays?:

Although the original poster mentioned "I don't need to consider holidays", if you DO happen to want to ignore holidays, just remember - "Holidays" is just an array of whatever dates you don't want to include and differs by country, region, company, person...etc.

Simply put the above code into a function that excludes/loops past the dates you don't want included. Something like this:

$tmpDate = '2015-06-22'; $holidays = ['2015-07-04', '2015-10-31', '2015-12-25']; $i = 1; $nextBusinessDay = date('Y-m-d', strtotime($tmpDate . ' +' . $i . ' Weekday'));  while (in_array($nextBusinessDay, $holidays)) {     $i++;     $nextBusinessDay = date('Y-m-d', strtotime($tmpDate . ' +' . $i . ' Weekday')); } 

I'm sure the above code can be simplified or shortened if you want. I tried to write it in an easy-to-understand way.

like image 68
Dave Avatar answered Oct 14 '22 09:10

Dave


For UK holidays you can use

https://www.gov.uk/bank-holidays#england-and-wales

The ICS format data is easy to parse. My suggestion is...

# $date must be in YYYY-MM-DD format # You can pass in either an array of holidays in YYYYMMDD format # OR a URL for a .ics file containing holidays # this defaults to the UK government holiday data for England and Wales function addBusinessDays($date,$numDays=1,$holidays='') {     if ($holidays==='') $holidays = 'https://www.gov.uk/bank-holidays/england-and-wales.ics';      if (!is_array($holidays)) {         $ch = curl_init($holidays);         curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);         $ics = curl_exec($ch);         curl_close($ch);         $ics = explode("\n",$ics);         $ics = preg_grep('/^DTSTART;/',$ics);         $holidays = preg_replace('/^DTSTART;VALUE=DATE:(\\d{4})(\\d{2})(\\d{2}).*/s','$1-$2-$3',$ics);     }      $addDay = 0;     while ($numDays--) {         while (true) {             $addDay++;             $newDate = date('Y-m-d', strtotime("$date +$addDay Days"));             $newDayOfWeek = date('w', strtotime($newDate));             if ( $newDayOfWeek>0 && $newDayOfWeek<6 && !in_array($newDate,$holidays)) break;         }     }      return $newDate; } 
like image 25
user2412642 Avatar answered Oct 14 '22 08:10

user2412642