首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用php中的mysql表字段值计算剩余天数?

如何使用php中的mysql表字段值计算剩余天数?
EN

Stack Overflow用户
提问于 2016-09-06 17:16:22
回答 2查看 75关注 0票数 0

我有一张名为“treat_period”的表,用来存储每只山羊接种疫苗的固定天数,还有一个order_details表,用于存储山羊购买时的信息。现在,treat_period表具有如下列,并具有相应的数据: id(1)、productID(山羊)、treat1(90)、treat2(180)、treat3(270)、treat4(360)、Peranalal热处理(365)。我想知道山羊是否已经分别呆了90天和180天。这是我的密码,请帮忙。

代码语言:javascript
复制
<?php
$treat_query = mysql_query("select * from treat_period where productID='$product_id'") or die(mysql_error());
                                    $treat_row = mysql_fetch_array($product_query);
 $treatperiod1=$treat_row['treat1'];
                                         $thisDate = date("Y-m-d");
                                         $allDays = $treatperiod1;
                                         $usedDays = round(abs(strtotime($thisDate)-strtotime($orderdate))/60/60/24);
                                         $Daysremaining = $allDays-$usedDays;
                                         if($Daysremaining==0)
                                          {
          echo "<a target = '_blank' href ='request_pay.php?order_id=$order_id&orderdate=$orderdate' class='btn btn-success'><i class='fa fa-pencil'></i>Vaccinate Gaoat</a>";
      }
      else
      {
          echo $Daysremaining.' Days Left for Vaccination';

}

                                       ?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-06 17:39:05

代码语言:javascript
复制
 $Daysremaining = ceil(abs(strtotime($thisDate) - strtotime($orderdate)) / 86400);
    if($treatperiod1==$Daysremaining){
       echo "<a target = '_blank' href ='request_pay.php?order_id=$order_id&orderdate=$orderdate' class='btn btn-success'><i class='fa fa-pencil'></i>Vaccinate Gaoat</a>";
    }else {
      echo $Daysremaining.' Days Left for Vaccination';
    }
票数 1
EN

Stack Overflow用户

发布于 2016-09-06 21:07:44

后来,我发现名为“treat_period”的表并不是在<?php echo $treat_row['treat1']; ?>之后替换存储的记录,但是如果我从order_details表中替换信息,那么就决定向order_details表(treat1、treat2等)添加更多的列,使用相同的代码,我现在可以得到预期的结果。请看这里的代码。

代码语言:javascript
复制
<?php
                                         $treatperiod1=$product_row['treat1'];
                                         $currentDate = date("Y-m-d");
                                         $totalDays = $treatperiod1;
                                         $usedDays = round(abs(strtotime($currentDate)-strtotime($orderdate))/60/60/24);
                                         $remainingDays = $totalDays-$usedDays;
                                         if($remainingDays==0)
                                          {
          echo "<a target = '_blank' href ='product_addon.php' class='btn btn-success'><i class='fa fa-pencil'></i>Vaccinate Gaoat</a>";
      }
      else
      {
          echo $remainingDays.' Days Left for Vaccination';

}

                                       ?>

谢谢

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39354514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档