php从数据库计算剩余天数
问题描述:
我有一个名为'treat_period'的表,用于存储每只山羊接种疫苗的固定天数以及存储购买山羊时信息的order_details表。 现在,treat_period表格具有以下列作为具有相应数据的列; id(1),productID(goat),treat1(90),treat2(180),treat3(270),treat4(360),annualtreat(365)。我想知道山羊是否分别停留90,180天。 这是我的代码,请帮忙。php从数据库计算剩余天数
<?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';
}
?>
答
$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';
}
+0
谢谢,但您的答案是给予2天而不是90天 - 这是假设还剩88天用于疫苗接种的2天。 – SUCCESSGANDE
答
后来我发现一个名为“treat_period”没有取代存储的记录是表<?php echo $treat_row['treat1']; ?>
之后,但如果我<?php echo $product_row['name']; ?>
这是从ORDER_DETAILS表中的信息获得移动如此决定添加更多的列到ORDER_DETAILS表( treat1,treat2等),使用相同的代码,我现在可以得到预期的结果。在此处查看代码。
<?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';
}
?>
感谢
所有的资本缺口看不好,我建议简单地用 “Successgande”。 – peterh