过去几天我一直在与这个问题作斗争,我被难住了。是时候把它带给大家了。
我的 JSON 数据如下所示:
{
"apiVersion": "0.1",
"data": {
"offset": 0,
"limit": 50,
"count": 50,
"total": 783,
"reports": [
{
"type": "ROOM_CHARGE",
"data": {
"datetime": "2014-11-04T14:00:27-08:00",
"originator": "639a",
"roomNumber": "639",
"chargeItem": "Premium Services",
"chargeAmountCents": 495
}
},
{
"type": "STB_EVENT_TIMED",
"data": {
"datetime": "2014-11-04T12:58:38-08:00",
"originator": "249b",
"roomNumber": "249",
"eventItem": "Watched movie 31008",
"duration": "P0Y0M0DT1H12M18.907S"
}
},
我可以很好地显示数据,但我正在尝试创建一种方法来从 JSON 中的 datetime
数据中搜索日期范围。
这是我尝试过的:
if(isset($_GET['submit_search'])){
$room_number = $_GET['id'];
$first_date = new DateTime($_GET['first_date']);
$last_date = new DateTime($_GET['last_date']);
}
<?php foreach($output1['data']['reports'] as $reporting): ?>
<!-- inside the date range -->
<?php $report_date = new DateTime($reporting['data']['datetime']); ?>
<?php if($first_date >= $report_date && $last_date <= $report_date): ?>
<?php if($reporting['type'] == "ROOM_CHARGE"): ?>
<tr>
<td><?php echo 'Room Charge'; ?></td>
<td><?php echo $report_date; ?></td>
<td><?php echo $report_date->format('Y-m-d'); ?></td>
<td><?php echo $report_date->format('h:i:s A'); ?></td>
<td><?php echo ($reporting['data']['roomNumber']) ?> </td>
<td><?php echo ($reporting['data']['originator']) ?> </td>
<td><?php echo ($reporting['data']['chargeItem']) ?></td>
<td><?php echo number_format(($reporting['data']['chargeAmountCents'])/100, 2, '.', ',') ?></td>
<td>-</td>
</tr>
<?php elseif($reporting['type'] == "STB_EVENT_TIMED"): ?>
<tr>
<td><?php echo 'Event'; ?></td>
<td><?php echo $report_date->format('Y-m-d'); ?></td>
<td><?php echo $report_date->format('h:i:s A'); ?></td>
<td><?php echo ($reporting['data']['roomNumber'])?> </td>
<td><?php echo ($reporting['data']['originator']) ?> </td>
<td><?php echo ($reporting['data']['eventItem']) ?></td>
<td>-</td>
<td><?php echo substr($reporting['data']['duration'], -10, 2) ?>:<?php echo substr($reporting['data']['duration'], -7, 2) ?>:<?php echo substr($reporting['data']['duration'], -4, 2) ?></td>
</tr>
<?php endif; ?>
<?php endif; ?>
<?php endforeach; ?>
$first_date
和 $last_date
设置为用户在表单中输入的任何内容,格式为 2014-11-05 xxxx-xx-xx
任何帮助将不胜感激。我可以提供更多代码,或您需要的任何其他内容。感谢您查看我的帖子。
最佳答案
无需对每个日期进行子字符串化,在这种情况下您可以使用 DateTime
类,然后您可以在 if 条件中比较它们:
简单的例子:
<?php
$json_string = '{"apiVersion": "0.1","data": {"offset": 0,"limit": 50,"count": 50,"total": 783,"reports": [{"type": "ROOM_CHARGE","data": {"datetime": "2014-11-04T14:00:27- 08:00","originator": "639a","roomNumber": "639","chargeItem": "Premium Services","chargeAmountCents": 495}},{"type": "STB_EVENT_TIMED","data": {"datetime": "2014-11-04T12:58:38- 08:00","originator": "249b","roomNumber": "249","eventItem": "Watched movie 31008","duration": "P0Y0M0DT1H12M18.907S"}}]}}';
$first_date = new DateTime($_GET['first_date'] . '-08:00'); // post/get user input values
$last_date = new DateTime($_GET['last_date'] . '-08:00');
$last_date->setTime(23, 59, 59);
$output1 = json_decode($json_string, true);
?>
<?php foreach($output1['data']['reports'] as $reporting): ?>
<!-- inside the date range -->
<?php $report_date = new DateTime($reporting['data']['datetime']); ?>
<?php if($report_date >= $first_date && $report_date <= $last_date): ?>
<?php if($reporting['type'] == "ROOM_CHARGE"): ?>
<tr>
<td><?php echo 'Room Charge'; ?></td>
<td><?php echo $report_date->format('Y-m-d'); ?></td>
<td><?php echo $report_date->format('h:i:s A'); ?></td>
<td><?php echo ($reporting['data']['roomNumber']) ?> </td>
<td><?php echo ($reporting['data']['originator']) ?> </td>
<td><?php echo ($reporting['data']['chargeItem']) ?></td>
<td><?php echo number_format(($reporting['data']['chargeAmountCents'])/100, 2, '.', ',') ?></td>
<td>-</td>
</tr>
<?php elseif($reporting['type'] == "STB_EVENT_TIMED"): ?>
<tr>
<td><?php echo 'Event'; ?></td>
<td><?php echo $report_date->format('Y-m-d'); ?></td>
<td><?php echo $report_date->format('h:i:s A'); ?></td>
<td><?php echo ($reporting['data']['roomNumber'])?> </td>
<td><?php echo ($reporting['data']['originator']) ?> </td>
<td><?php echo ($reporting['data']['eventItem']) ?></td>
<td>-</td>
<td><?php echo substr($reporting['data']['duration'], -10, 2) ?>:<?php echo substr($reporting['data']['duration'], -7, 2) ?>:<?php echo substr($reporting['data']['duration'], -4, 2) ?></td>
</tr>
<?php endif; ?>
<?php endif; ?>
<?php endforeach; ?>
</table>
<?php endif; ?>
补充信息:
一旦它是一个 DateTime
对象,您现在可以使用它的方法:
<td><?php echo $report_date->format('Y-m-d'); ?></td>
<td><?php echo $report_date->format('h:i:s A'); ?></td>
关于php - 创建一种从 JSON 日期时间搜索日期范围的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26769574/