Created
March 30, 2020 02:57
-
-
Save alucard001/29f55a2e79f3d4a76ff73db95fdfe7db to your computer and use it in GitHub Desktop.
[PHP]Get Next Business Day, include/not include holiday
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* One of my task is to create a function that find the next business day. | |
* | |
* Reference: https://stackoverflow.com/a/5532070/1802483 | |
*/ | |
class GetNextBusinessDay{ | |
// Get a list of holiday in https://holidayapi.com/. Then I copy, paste and edit it manually to make this list | |
private $holidayList = [ | |
"2020-04-04", | |
"2020-04-10", | |
"2020-04-11", | |
"2020-04-12", | |
"2020-04-13", | |
"2020-04-30", | |
"2020-05-01", | |
"2020-05-10", | |
"2020-06-20", | |
"2020-06-21", | |
"2020-06-25", | |
]; | |
private $returnDateFormat = "Ymd"; | |
public function find($tmpDate){ | |
// T+2 (business date), if you need T + x (current day plus some number of days), you can edit it here. | |
// In my case since I am using T+2, so I put $i + 2 | |
$i = 1 + 2; | |
// Need to have more limit: before 5:00pm, if current time is bigger than deadLine, move to next day | |
// if you need it you can keep it, or you can remove it. :D | |
$deadLine = strtotime(date("Y-m-d 17:00", strtotime($tmpDate))); | |
if(strtotime("now") >= $deadLine){ | |
$i += 1; | |
} | |
// The core of this script is "+x Weekday" | |
$nextBusinessDay = date($this->returnDateFormat, strtotime($tmpDate . ' +' . $i . ' Weekday')); | |
// If the date is in the holidayList above, move to next day, and check again | |
// Include this checking if you need to verify date with your holiday list, | |
// or you can just comment it to remove. | |
while (in_array($nextBusinessDay, $this->holidayList)) { | |
$i++; | |
$nextBusinessDay = date($this->returnDateFormat, strtotime($tmpDate . ' +' . $i . ' Weekday')); | |
} | |
return $nextBusinessDay; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I know I can make it more elegant and simple, and class/object oriented. But I think it is just enough for people to understand the login behind.
Hope it helps someone.