Home > Software engineering >  How to calculate if date range is within this week
How to calculate if date range is within this week

Time:12-23

I use an API that returns the following data:

{
"count": 3,
"data": [
    {
        "id": "11ec62fcfc82e511b06a756430fa6a9e",
        "type": "HOLIDAY",
        "from": "2021-12-13",
        "to": "2021-12-19",
        "resourceId": "11ec46d6abeeeb08b8f937e6be68fdf0",
        "createdAt": "2021-12-22T07:58:44"
    },
    {
        "id": "11ec5e81746041328bd6f1d234a6c496",
        "type": "ILLNESS",
        "from": "2021-12-15",
        "to": "2021-12-17",
        "resourceId": "11ec46e7a62ab8f6b8f937e6be68fdf0",
        "createdAt": "2021-12-16T03:04:23"
    },
    {
        "id": "11ec62ff1df2654d8bd6f1d234a6c496",
        "type": "HOLIDAY",
        "from": "2021-12-24",
        "to": "2021-12-24",
        "resourceId": "11ec46d6547a00728be3e1ed8ff29535",
        "createdAt": "2021-12-22T08:14:00"
    }
],
"success": true
}

These are vacation and sickness data. I have a weekly report where I need to calculate the number of days in vacation/sickness during that week for the particular "ressourceID". How can I calculate how many days in the "from"-"to" range there are within a particular week?

I have no approach on how this can be done easily. I was thinking about https://www.php.net/manual/de/datetime.format.php to convert it into "z" format, but i wasn't able to find an easy way for that, either.

Let me add an example of what i am looking for, i think the question wasn't clear:

Let's imagine we have the following week:

Considered Week: 20.12. - 24.12.

Now we could have the following cases for absences and how they should be considered

Case 1: Absence starts before Weekstart and end within week

Absence: 15.12. - 21.12.

=> In this case we have 2 days that are included in the week

Case 2: Absence starts before Weekstart and end after week

Absence: 15.12. - 26.12.

=> In this case we have 5 days that are included in the week

Case 3: Absence starts within Week and end within week

Absence = 20.12. - 22.12.

=> In this case we have 3 days that are included in the week

Case 4: Absence starts within Week and end after week

Absence: 21.12. - 29.12.

=> In this case we have 4 days that are included in the week

CodePudding user response:

The following code will give week number based on FROM DATE & TO DATE GIVEN.

    $from = new \DateTime('2020-12-15');
    $to = new \DateTime('2020-12-17');
    $weeks = date_diff($to,$from)->days/7;

If the result is 0 then it is the first week.

Or if you want to get the weeks based on FROM DATE to CURRENT DATE.

    $from = new \DateTime('2020-12-15');
    $to = new \DateTime();
    $weeks = date_diff($to,$from)->days/7;

CodePudding user response:

This will loop threw array and check which date is created this week hope this helps

// This is the json string that was returned
$jsonString = '{"count":3,"data":[{"id":"11ec62fcfc82e511b06a756430fa6a9e","type":"HOLIDAY","from":"2021-12-13","to":"2021-12-19","resourceId":"11ec46d6abeeeb08b8f937e6be68fdf0","createdAt":"2021-12-22T07:58:44"},{"id":"11ec5e81746041328bd6f1d234a6c496","type":"ILLNESS","from":"2021-12-15","to":"2021-12-17","resourceId":"11ec46e7a62ab8f6b8f937e6be68fdf0","createdAt":"2021-12-16T03:04:23"},{"id":"11ec62ff1df2654d8bd6f1d234a6c496","type":"HOLIDAY","from":"2021-12-24","to":"2021-12-24","resourceId":"11ec46d6547a00728be3e1ed8ff29535","createdAt":"2021-12-22T08:14:00"}],"success":true}';

// This will decode the json string make an array
$data = json_decode($jsonString, true);

// Function to check date was created this week
function checkWeek($date) {
    // will get current date and - 7 days
    $ThisWeek = time() - (7 * 24 * 60 * 60);
    
    // will change from date from string to timestamp
    $DateCreated = strtotime($date);
    
    // will check if the createdDate > this week
    if($DateCreated >= $ThisWeek) {
        echo $date . ", Was Created This Week.\n";
        return true;
    }
    return false;
}

// Loop threw all values and check which one is created this week
foreach ($data["data"] as &$value) {
    checkWeek($value["createdAt"]);
}
  • Related