【问题标题】:Calculate intersection between 2 date ranges计算 2 个日期范围之间的交集
【发布时间】:2022-01-24 12:19:24
【问题描述】:

我想计算特定周内的空闲天数(缺勤)。我使用返回以下数据的 API:

    {
    "count": 1,
    "data": [
        {
            "id": "11ec62ff1df2654d8bd6f1d234a6c496",
            "type": "HOLIDAY",
            "from": "2021-12-22",
            "to": "2021-12-23",
            "resourceId": "11ec46d6547a00728be3e1ed8ff29535",
            "createdAt": "2021-12-22T08:14:00"
        }
    ],
    "success": true
    }

这些是假期和疾病数据。我有一份每周报告,我需要计算那一周的缺勤天数。我需要找到一种简单的方法来计算一周中的缺勤天数。

我尝试过使用https://www.php.net/manual/de/datetime.format.php 并将其转换为“z”格式,但它看起来并不优雅,从性能角度来看,我认为它不是最好的。


//The week range 
$weekStart = new DateTime("2021-12-20");
$weekEnd = new DateTime("2021-12-24");

//The Planned absence
$absenceStart = new DateTime("2021-12-22");
$absenceEnd = new DateTime("2021-12-23");

//Specify the DateInterval for calculating the period
$interval = DateInterval::createFromDateString('1 day');

//Need to add the interval to the end date in order to consider the end as well
$weekEnd->add($interval);
$absenceEnd->add($interval);

//Getting the 2 periods week and absence
$weekPeriod = new DatePeriod($weekStart, $interval, $weekEnd);
$absencePeriod = new DatePeriod($absenceStart, $interval, $absenceEnd);


$weekArray = array();
$absenceArray = array();

//put the day number format('z') into an array of the week
foreach ($weekPeriod as $i => $dt) {
    $weekArray[$i] = $dt->format('z');
}

//put the day number format('z') into an array of the absence
foreach ($absencePeriod as $i => $dt) {
    $absenceArray[$i] = $dt->format('z');
}

//get the intersection between both arrays
$ergebnis = array_intersect($weekArray, $absenceArray);

//calculate the number of entries
echo "The employee has <b>".count($ergebnis)."</b> free days in the week from 2021-12-20 until 2021-12-24";

这是返回正确的信息。

The employee has 2 free days in the week from 2021-12-20 until 2021-12-24

任何人都可以建议是否有更好的方法,或者我是否可以至少对其进行调整以使其更优雅和性能更好?

非常感谢

【问题讨论】:

    标签: php arrays datetime


    【解决方案1】:

    如果不需要该信息,您可以消除对缺席数组和第二周数组循环的循环,而不是循环遍历周数组、缺席数组,然后再次遍历周数组 (array_intersect)。所以你只有 1 个循环而不是 3 个应该更高效。

    //The week range 
    $weekStart = new DateTime("2021-12-20 00:00:00");
    $weekEnd = new DateTime("2021-12-24 23:59:59");
    
    //The Planned absence
    $absenceStart = new DateTime("2021-12-22 00:00:00");
    $absenceEnd = new DateTime("2021-12-23 23:59:59");
    
    //Specify the DateInterval for calculating the period
    $interval = DateInterval::createFromDateString('1 day');
    
    //Getting the available period
    $weekPeriod = new DatePeriod($weekStart, $interval, $weekEnd);
    
    $availableDayCount = 0;
    
    //put the day number format('z') into an array of the week
    foreach ($weekPeriod as $dt) {
        // Filter out days the employee is absent.
        if($dt < $absenceStart || $dt > $absenceEnd) {
            $availableDayCount += 1;
        }
    }
    
    //calculate the number of entries
    echo "The employee has <b>" . $availableDayCount . "</b> free days in the week from " . $weekStart->format('Y-m-d') . " until " . $weekEnd->format('Y-m-d');
    

    此外,它似乎没有提供正确的信息(也许我错了?)。你的问题是 2 天,但我数 3:

    • 2021-12-20
    • 2021-12-21
    • 2021-12-24

    (22号和23号缺席)

    如果这不正确,您可以将 $weekEnd 更改为 2021-12-23(我添加了一天的开始/结束时间,以便计算一整天)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-20
      • 2015-08-06
      • 2020-07-01
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      相关资源
      最近更新 更多