正在开发一个小型应用程序,该应用程序在两个设定的日期范围内不包括周末和公共假期。例如,如果用户输入2019-05-01和2019-05-10,它将返回不包含周末的范围,并且如果有任何公共假日,则将结果排除在结果之外。
我从两个变量中获得日期范围以及每天的时间和工作时间
$duration = $endTime - $startTime;
如何使用in_array 当我在2019-05-01和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);
需要计算这两个日期之间的总时间,不包括周末和公共假期
答案 0 :(得分:1)
您没有说,所以我假设周末工作。只需检查$holidays
数组:
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");
}
}