2014-11-06 54 views
2

过去几天我一直在与这个问题作斗争,而且我很难过。有时间把它带给你。创建一种搜索JSON日期范围的方法datetime

我的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月5日为xxxx-xxxx

任何帮助将不胜感激。我可以提供更多的代码或其他任何你需要的东西。感谢您检查我的帖子。

回答

1

无需串每个日期,你可以在这种情况下使用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> 
+0

感谢您的尝试。当然,我很感激你的帮助。我尝试使用<?php if($ report_date> = $ first_date && $ report_date <= $ last_date):?>,并且出现语法错误“unexpected”&&'(T_BOOLEAN_AND)“。我绝对明白你在这里说的话,我会继续努力。 – ValleyDigital 2014-11-06 00:34:51

+0

@ValleyDigital你确定你把它复制了吗?在这里检查它http://codepad.viper-7.com/xo7PvW – Ghost 2014-11-06 00:41:32

+0

我复制它的权利。不过,我仍在努力。我不太了解DateTime。当我尝试回显report_date时,我得到“可捕获的致命错误:DateTime类的对象无法转换为字符串”。你的代码工作,这是我的错,对不起。 – ValleyDigital 2014-11-06 00:45:37