【发布时间】:2014-11-06 00:09:09
【问题描述】:
过去几天我一直在与这个问题作斗争,我很困惑。是时候把它带给大家了。
我的 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
任何帮助将不胜感激。我可以提供更多代码,或者您需要的任何其他内容。感谢您查看我的帖子。
【问题讨论】:
标签: php html arrays json datetime