0

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. i am trying to build some logic that it fetches the data of some specific dates i.e data of within 72 hours. Date is given in the file in "1/12/2020" format. 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). 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

"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.

3
  • Your sample does not contain anything like $data["Messages"] Please make your data sample make sence Commented Jul 31, 2020 at 14:03
  • $data = json_decode($str_data, true); and the "Message" is the array name in json file Commented Jul 31, 2020 at 14:12
  • As your json file has only a date and no time, 72 hours makes it a little difficult to be accurate. Do you mean show only the data with a date of today, yesterday and the day before that? Commented Jul 31, 2020 at 15:16

1 Answer 1

1

If you were to use the DATETIME object in PHP and then use the ->diff() difference method you might do it like this.

You will also have to convert the date seperator from / to - so that the DateTime class/functions see that date correctly. a / will make them asume an American Format date, and these dates are not US Format

$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;
    } 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.