Working on a small app which excludes weekends and public holidays from two set dates range. ie if user enters 2019-05-01 and 2019-05-10 it returns the range without the weekends and if there are any public holidays exclude those from results.
i get the date range with the time per day, working hours from two variables
$duration = $endTime - $startTime;
How can i use in_array to results returned when i print date between 2019-05-01 and 2019-05-09
//date range
Array
(
[2019-05-01] => 09:30:00
[2019-05-02] => 09:30:00
[2019-05-03] => 09:30:00
[2019-05-06] => 09:30:00
[2019-05-07] => 09:30:00
[2019-05-08] => 09:30:00
[2019-05-09] => 09:30:00
)
// public holiadys
Array
(
[0] => 2019-05-08
[1] => 2019-05-09
)
$start = new DateTime('2019-05-01');
$end = new DateTime('2019-05-31');
// getting the day interval https://php.net/manual/en/class.dateinterval.php
$day = new DateInterval("P1D");
//holidays hardcoded for testing, must create database abd select dates from there
$holidays = array('2016-01-01','2016-03-25');
$days = array();
//hours set to 8 hours a daye
$startTime = new DateTime('08:00:00');
$endTime = new DateTime('17:30:00');
$duration = $startTime->diff($endTime);
$data = $endTime - $startTime;
foreach(new DatePeriod($start, $day, $end->add($day)) as $day => $k) {
$day_num = $k->format("N");
$w = $day_num;
if($w < 6) { //if its wmore than 5 its weekend
$days[$k->format("Y-m-d")] = $duration->format("%H:%I:%S");
}
}
foreach ($holidays as $value) {
$value = $value;
}
print_r($days);
print_r($holidays);
need to calculate the total time between those dates excluding weekend and public holidays
You didn't say, so I will assume that the weekend part is working. Just check the $holidays
array:
foreach(new DatePeriod($start, $day, $end->add($day)) as $k) {
$dnm = $k->format("N");
$key = $k->format("Y-m-d")
if($dnm < 6 && !in_array($key, $holidays)) {
$days[$key] = $duration->format("%H:%I:%S");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.