首页 > 解决方案 > 如何从数据库中显示 3 个月内的 div 内容

问题描述

我有到 date1 的数据库,并且 date1 我已经添加了 3 个月,那是 date2 我为 if 条件编写了代码但不工作

我已经在工作并编写了一些代码但不工作

$createDate = new DateTime($ac_join);
$strip = $createDate->format('d-m-Y');
$effectiveDate = date('d-m-Y', strtotime("+3 months", strtotime($last)));

输出将是这样的

$last Value : 25-04-2019
添加 3 个月后 : 27-07-2019

 if($effectiveDate > $last )
{
   // last value to 3 months content not display
}

我预计输出是如果条件将满足 3 个月后仅显示内容其他明智的剩余值显示

这是代码

<?php $sql ="SELECT * from `donor_register` ORDER BY `dnr_blood_donate` LIMIT 4 ";
   $result = $conn->query($sql);
   $dnrcount   = $result->num_rows;
   while($row = $result->fetch_assoc()){

   $last =  $row['dnr_blood_donate']; 
   $createDate = new DateTime($ac_join);
   $strip = $createDate->format('d-m-Y');
   $effectiveDate = date('d-m-Y', strtotime("+3 months", strtotime($last)));

   if($effectiveDate > $last )     
    {  ?>
      <div class="col-xl-3 col-lg-3 col-md-6">
         <div class="single-member">
            <a href="donor-profile.php?did=<?=$did;?>">
               <div class="part-img">
                  <img src="assets/image/donor/<?=$image;?>" alt="Donor image" style="height: 250px; width: 250px;"/>
               </div>
            </a>
            <div class="part-text">
               <a href="donor-profile.php?did=<?=$did;?>">
                  <h3><?=$fullname;?></h3>
               </a>
               <h4>Blood group : (<?=$bgroup;?>)</h4>
            </div>
            <div class="part-social">
               Last Blood Donation : <?=$last;?>
            </div>
         </div>
      </div>
<?php $i++; } ?>

标签: javascriptphpmysqli

解决方案


我认为您想计算两个日期之间的延迟,如果延迟超过 3 个月,请执行操作。在我的示例中,date_1等于25-04-2019并且date_2等于now。我想知道是否从date_1now过去 3 个月。

这是您的代码:

$date1 = new DateTime("25-04-2019");   //****** This is date_1 and in your code, you get this date from the database.
$date2 = new DateTime();    //****** This is date_2, I assume it is now.

$periodOfTime = 90;  //****** 3 months

if ($date1->diff($date2)->days > $periodOfTime) {
    echo "Three months passed from the date_1";
}
else {
    echo "Less than three months passed from the date_1";
}

推荐阅读