在php中对json格式数据进行日期时间验证 [英] date time validations on json format data in php
问题描述
我对php很陌生,所以我一直在尝试制作一个基本的简单应用程序,该应用程序读取json文件并将数据从该文件中提取到您的应用程序中。我正在尝试建立一些逻辑,以获取某些特定日期的数据,即72小时内的数据。在文件 1/12/2020中给出日期。格式。我试图转换以秒为单位的json日期,并用系统日期(以秒为单位)减去它,然后将该差异日期(系统日期-json文件中给出的日期数据)与72小时(以秒为单位)进行比较。但是我做不到。
这就是我尝试过的内容
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>";
?>
这是示例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.
推荐答案
如果要在PHP中使用DATETIME对象,然后使用-> diff()
差异方法,您可能会这样。
If you were to use the DATETIME object in PHP and then use the ->diff()
difference method you might do it like this.
您还必须将日期分隔符从 /
到-
,以便DateTime类/函数正确看到该日期。 /
将使它们成为美国格式的日期,而这些日期不是美国格式的
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;
}
}
这篇关于在php中对json格式数据进行日期时间验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!