我正在遍历并填充表的API返回了数据。我只对过去三十天的数据感兴趣。
我写了一些代码,该代码采用今天的日期,从中减去30天并存储结果。然后,它会创建一个新数组,遍历API数据并比较每个条目的日期,而不管边界之外的任何内容。
$response = curl_exec($curl);
$err = curl_error($curl);
$responseResult = json_decode($response, true);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$today = date('Y-m-j');
$newdate = strtotime ( '-30 days' , strtotime ( $today ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
foreach($responseResult as $subject) {
$createdDate = $subject['created_at'];
if($createdDate < $newdate) {
$filteredThirtyDays[] = $createdDate;
}
}
return count($filteredThirtyDays);
这不起作用。我的逻辑是错误的。
{
"id": "5cf65651bbddbd5462599808",
"number": 4551,
"user_id": "5b8425c8e694aa3c6a835b67",
"state": "closed",
"subject": "Database",
"label_ids": [
"5c49b2d9bbddbd17834bbff6"
],
"customer_id": "5b52f0cabbddbd01c5bdd6c8",
"type": "email",
"reply_to": "user@domain.com",
"reply_cc": "",
"group_id": "5a5f65fed5593070120b9779",
"inbox_id": "5a5f65fed5593070120b9779",
"updated_at": "2019-06-04T13:41:58Z",
"created_at": "2019-06-04T11:30:25Z",
"spam": false,
"trash": false,
"summary": "",
"rating": null,
"waiting_since": "2019-06-04T12:56:05Z"
},
{
"id": "5cf6561ebbddbd28217a0b89",
"number": 4550,
"user_id": "5b51bf13bbddbd7e6248c934",
"state": "closed",
"subject": "Exchange",
"label_ids": [
"5c4b0cf4bbddbd48cd69e68a"
],
"customer_id": "5b51fdf1bbddbd5f4575a428",
"type": "phone",
"reply_to": "user@domain.com",
"reply_cc": "",
"group_id": "5a5f65fed5593070120b9779",
"inbox_id": "5a5f65fed5593070120b9779",
"updated_at": "2019-06-04T12:49:06Z",
"created_at": "2019-06-04T11:29:35Z",
"spam": false,
"trash": false,
"summary": "Hi Chris Might have been the user name",
"rating": null,
"waiting_since": "2019-06-04T12:41:08Z"
}
]
我要计算的是30天之内有多少物品。我得到的是所有日期的所有结果。
我认为问题在于时间格式。 API以字符串形式返回日期,格式为 2019-06-04T12:49:06Z 。当我执行 echo strtotime('2019-06-04T12:49:06Z'); 时,它会返回 1559652546 ,这就是正在比较的内容。
所以,我想我需要将Zulu时间格式转换成可以用来比较的内容...
答案 0 :(得分:1)
检查它是否适合您。
<? php
$responseResult = '[{
"id" : "5cf65651bbddbd5462599808",
"number": 4551,
"user_id": "5b8425c8e694aa3c6a835b67",
"state": "closed",
"subject": "Database",
"label_ids": [
"5c49b2d9bbddbd17834bbff6"
],
"customer_id": "5b52f0cabbddbd01c5bdd6c8",
"type": "email",
"reply_to": "user@domain.com",
"reply_cc": "",
"group_id": "5a5f65fed5593070120b9779",
"inbox_id": "5a5f65fed5593070120b9779",
"updated_at": "2019-06-04T13:41:58Z",
"created_at": "2019-06-04T11:30:25Z",
"spam": false,
"trash": false,
"summary": "",
"rating": null,
"waiting_since": "2019-06-04T12:56:05Z"
}, {
"id": "5cf6561ebbddbd28217a0b89",
"number": 4550,
"user_id": "5b51bf13bbddbd7e6248c934",
"state": "closed",
"subject": "Exchange",
"label_ids": [
"5c4b0cf4bbddbd48cd69e68a"
],
"customer_id": "5b51fdf1bbddbd5f4575a428",
"type": "phone",
"reply_to": "user@domain.com",
"reply_cc": "",
"group_id": "5a5f65fed5593070120b9779",
"inbox_id": "5a5f65fed5593070120b9779",
"updated_at": "2019-06-04T12:49:06Z",
"created_at": "2019-05-04T11:29:35Z",
"spam": false,
"trash": false,
"summary": "Hi Chris Might have been the user name",
"rating": null,
"waiting_since": "2019-06-04T12:41:08Z"
}]
';
$today = date('Y-m-j');
$filteredThirtyDays = array();
$responseResultArr = json_decode($responseResult);
foreach($responseResultArr as $created => $date) {
$nDate = (array) $date;
if (strtotime($nDate['created_at']) <= strtotime('-30 days')) {
$filteredThirtyDays[] = $date;
}
}
print_r($filteredThirtyDays);