我使用回傳以下資料的 API:
{
"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
}
這些是假期和疾病資料。我有一個每周報告,我需要計算特定“ressourceID”在該周內休假/生病的天數。如何計算特定周內“從”-“到”范圍內的天數?
我不知道如何輕松地做到這一點。我正在考慮https://www.php.net/manual/de/datetime.format.php將其轉換為“z”格式,但我也無法找到一種簡單的方法。
讓我添加一個我正在尋找的例子,我認為這個問題不清楚:
假設我們有以下一周:
考慮周:20.12。- 24.12。
現在我們可以有以下缺勤情況以及應如何考慮
案例 1:缺勤在 Weekstart 之前開始并在一周內結束
缺席:15.12。- 21.12。
=> 在這種情況下,我們有2天包含在一周中
案例 2:缺勤在 Weekstart 之前開始并在一周之后結束
缺席:15.12。- 26.12。
=> 在這種情況下,我們有5天包含在一周中
案例 3:缺勤在一周內開始并在一周內結束
缺席 = 20.12。- 22.12。
=> 在這種情況下,我們有3天包含在一周中
案例 4:缺勤在一周內開始并在一周后結束
缺席:21.12。- 29.12。
=> 在這種情況下,我們有4天包含在一周中
uj5u.com熱心網友回復:
以下代碼將根據 FROM DATE & TO DATE GIVEN 給出周數。
$from = new \DateTime('2020-12-15');
$to = new \DateTime('2020-12-17');
$weeks = date_diff($to,$from)->days/7;
如果結果為 0,則為第一周。
或者,如果您想根據 FROM DATE 到 CURRENT DATE 獲取周數。
$from = new \DateTime('2020-12-15');
$to = new \DateTime();
$weeks = date_diff($to,$from)->days/7;
uj5u.com熱心網友回復:
這將回圈拋出陣列并檢查本周創建的日期希望這有幫助
// 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"]);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/390162.html
