简体   繁体   English

php 中 json 格式数据的日期时间验证

[英]date time validations on json format data in php

I am very new to php so i have been trying to make a basic simple application that reads the json file and fetches that data into your application from that file.我对 php 非常陌生,所以我一直在尝试制作一个基本的简单应用程序,它可以读取 json 文件并将该数据从该文件中提取到您的应用程序中。 i am trying to build some logic that it fetches the data of some specific dates ie data of within 72 hours.我正在尝试构建一些逻辑来获取某些特定日期的数据,即 72 小时内的数据。 Date is given in the file in "1/12/2020" format.日期以“2020 年 1 月 12 日”格式在文件中给出。 i was trying to convert the json date in seconds and subtracting it with system date(in seconds) and then comparing that difference date(system date - date data given in json file) with 72 hours (in seconds).我试图以秒为单位转换 json 日期并将其与系统日期(以秒为单位)相减,然后将差异日期(系统日期 - json 文件中给出的日期数据)与 72 小时(以秒为单位)进行比较。 but i couldn't do so.但我不能这样做。 here's what i have tried这是我尝试过的

<?php
$str_data = file_get_contents("json_response.json");
$data = json_decode($str_data, true);

echo "<div class='container-fluid'>
        <ul class='w3-ul w3-card-4'>";

for($i = 0; $i < sizeof($data["Messages"]); $i++) {
        
    $id=$data["Messages"][$i]["id"];
    $pnum=$data["Messages"][$i]["phonenumber"];
    $body=$data["Messages"][$i]["body"];
    $m_date=$data["Messages"][$i]["M_date"];
    $is_read=$data["Messages"][$i]["isRead"];
    $M_date_inSecs = strtotime($m_date);
    $system_date_inSecs = strtotime("now") ;
    $difference_time = $system_date_inSecs - $M_date_inSecs;
        
    if($is_read=="false" && $difference_time <= strtotime("72 hours") )
        echo " 
            <li class='w3-bar'>
              <span onclick='this.parentElement.style.display=\"none\"'class='w3-bar-item w3-button w3-white w3-large w3-right'>×</span>
              <table class='float-right text-secondary'>
              <tr><td>$m_date</td></tr>
              <tr><td>Read Status: $is_read</td></tr>
              </table>
              <img src='profile.png' class='w3-bar-item w3-circle w3-hide-small' style='width:75px'>
              <div class='w3-bar-item'>
                <span class='w3-large'>{$id}:{$pnum} </span><br>
                
                <span style='max-height:60px;overflow:auto;max-width:800px;display:block;'>$body</span>
              </div>
            </li>";
}

echo "</ul></div>";
?>

here's the sample json data这是样本 json 数据

"Messages":[
    {
        "id":"0",
        "phonenumber":"Sannan ITU",
        "body":"Manan jaldi aja lecture bhi hai is ka 1:45",
        "M_date":"31/7/2020",
        "isRead":"false"
    },
]
}

so where's i am doing wrong.所以我在哪里做错了。 Any suggestion would be really appreciated.任何建议将不胜感激。

If you were to use the DATETIME object in PHP and then use the ->diff() difference method you might do it like this.如果您要在 PHP 中使用 DATETIME object 然后使用->diff()差异方法,您可能会这样做。

You will also have to convert the date seperator from / to - so that the DateTime class/functions see that date correctly.您还必须将日期分隔符从/转换为-以便 DateTime 类/函数正确地看到该日期。 a / will make them asume an American Format date, and these dates are not US Format a /将使他们采用美国格式日期,并且这些日期不是美国格式

$now = new DateTimeImmutable('now');
foreach ($data['Messages'] as $msg){
    
    $jd = new DateTime(str_replace('/','-', $msg['M_date']));

    $diff = $now->diff($jd);
    if ( $diff->d <= 2 ){
        echo $msg['id'] . ' -- ' . $jd->format('Y-m-d').PHP_EOL;
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM