Igbeh Emmanuel
Igbeh Emmanuel

Reputation: 53

How Can I Calculate Remaining Days using mysql table field value in php

I have a table named 'treat_period' for storing fix number of days for each goat to reach for vaccination and an order_details table for storing information for when the goat was purchased. Now the treat_period table has the following as columns with corresponding data; id(1), productID(goat), treat1(90), treat2(180), treat3(270), treat4(360), annualtreat(365). I want to know whether the goat has stays for 90, 180 days respectively. Here is my code, Please help.

<?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';

}

                                       ?>

Upvotes: 0

Views: 104

Answers (2)

Igbeh Emmanuel
Igbeh Emmanuel

Reputation: 53

I later discovered that table named 'treat_period' was not displacing stored records after <?php echo $treat_row['treat1']; ?> but if I <?php echo $product_row['name']; ?> which is from the order_details table the information get displace so decided to add more columns to order_details table (treat1, treat2 etc), using the same code I can now get the expected result. See code here.

<?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';

}

                                       ?>

thanks

Upvotes: 1

Ankneema
Ankneema

Reputation: 35

 $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';
    }

Upvotes: 1

Related Questions