我查阅了返回以下数据的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
}
这些是假期和疾病数据。我有一份每周报告,我需要计算在那一周的假期/疾病天数,以备不时之需。我有点困惑,我怎么能计算出“从”-“到”范围在一个特定的星期内有多少天。
我没有办法轻易做到这一点。我在想https://www.php.net/manual/de/datetime.format.php把它转换成“z”,但我找不到一个简单的方法。
以下代码将根据起始日期给出周数
$from = new \DateTime('2020-12-15');
$to = new \DateTime('2020-12-17');
$weeks = date_diff($to,$from)->days/7;
如果结果为0,则这是第一周。
或者,如果您希望根据从日期到当前日期获取周数。
$from = new \DateTime('2020-12-15');
$to = new \DateTime();
$weeks = date_diff($to,$from)->days/7;