【问题标题】:Create a way to search a date range from JSON datetime创建一种从 JSON 日期时间搜索日期范围的方法
【发布时间】: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


    【解决方案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>
    

    【讨论】:

    • 感谢您的尝试。我很感激你的帮助,当然。我尝试使用 = $first_date && $report_date 并且出现语法错误“意外的'&&' (T_BOOLEAN_AND)”。我肯定明白你在说什么,我会继续努力。
    • @ValleyDigital 你确定你复制对了吗?在这里查看codepad.viper-7.com/xo7PvW
    • 我抄对了。不过,我仍在努力。我不太了解 DateTime 。当我尝试回显 report_date 时,我得到“可捕获的致命错误:DateTime 类的对象无法转换为字符串”。你的代码有效,这是我的错,对不起。
    • @ValleyDigital 是的,这是正确的行为,当它现在是 DateTime 对象时,它现在是一个对象,因此您不能再直接回显它,因为它不是字符串。不要将日期作为字符串进行比较,将它们转换为秒或使用 datetime 对象,因为它们在 if 条件中可以直接比较
    • 好的,我明白了。我可以比较它们,然后仍然回显子字符串?
    猜你喜欢
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 2015-09-17
    • 2019-06-12
    • 1970-01-01
    相关资源
    最近更新 更多