0

I have a form with a dynamic select option. I want to calculate the age of a goat in months from its date of birth after selecting it from the db and echo the selected goat age on the following form field that follows the Dynamic selection option. Here is the selection script:

<?php 
$conn = mysqli_connect("localhost", "root", "xxx", "xxxx");
if(mysqli_connect_errno($conn)) {
echo "Unable to connect to database server";
}

$sql = "SELECT * FROM goats WHERE sex='Male'";
$query = mysqli_query($conn, $sql);

echo '<select name="hegoat">';
echo '<option value="">Choose He Goat</option>';
while($hegoats = mysqli_fetch_assoc($query)){
echo "<option>{$hegoats['goatid']}</option>";
}
echo '</select>';
?>

Data Base table is goats with 'dob' as the column for Date of Birth while the form field for age is:<input type="text" name="age" id="age"/>

And my PHP code is

<?php
if(isset($_POST['sub'])) 
{ 
    date_default_timezone_set ("Asia/Calcutta");
    $dateofreg1=date("d M Y");
    $dbd=$_POST['dob'];
    $startTimeStamp = strtotime($dbd);
    $endTimeStamp = strtotime($dateofreg1);
    $timeDiff = abs($endTimeStamp - $startTimeStamp);
    $numberDays = $timeDiff/86400;
    $numberDays = intval($numberDays);
    $days="Total day :".$numberDays;
    $birthDate =$dbd; 
    $birthDate = explode("/", $birthDate);
    $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md") ? ((date("Y") - $birthDate[2]) - 1) : (date("Y") - $birthDate[2]));
    $dobs="Age is:" . $age; 
}
?>
  • 1
    see [**Find Month difference in php?**](http://stackoverflow.com/q/2681548/1407478) - this is basically what you are asking. Goats, people or apples - it is the same deal :) – davidkonrad Oct 03 '15 at 08:10
  • What is the format of date_pf_birth column? is it date or timestamp? – sandeepsure Oct 03 '15 at 08:12
  • Ahh, so the logical somewhat would be to echo all the `hegoats['goatid']` with their respective `values` in the ` – DirtyBit Oct 03 '15 at 08:12
  • the date_of_birth column is Varchar – Igbeh Emmanuel Oct 03 '15 at 08:14
  • 1
    Calculating the age of a goat is a lot like calculating the age of anything. That said, if you're storing dates, store dates, not strings – Strawberry Oct 03 '15 at 08:14
  • ` date("md") ? ((date("Y") - $birthDate[2]) - 1) : (date("Y") - $birthDate[2])); $dobs="Age is:" . $age; }?>` – Igbeh Emmanuel Oct 03 '15 at 08:31
  • the above code does not show the number of days but only shows in years. please help – Igbeh Emmanuel Oct 03 '15 at 08:33
  • @IgbehEmmanuel : Answered as per your latest comments. Try. – Subin Thomas Oct 03 '15 at 08:47

2 Answers2

1

As per your last comment, I am giving this answer.

 <?php if(isset($_POST['sub'])) 
    {
     date_default_timezone_set ("Asia/Calcutta");
    $dateofreg1=date("d M Y");
    $dbd=$_POST['dob'];
    $startTimeStamp = strtotime($dbd);
    $endTimeStamp = strtotime($dateofreg1);

    $year1 = date('Y', $startTimeStamp);   //select year1
    $year2 = date('Y', $endTimeStamp);    //select year 2
    $month1 = date('m', $startTimeStamp);   //month1
    $month2 = date('m', $endTimeStamp);      //month2
    $diff = (($year2 - $year1) * 12) + ($month2 - $month1);  //year*12 and month difference
     $dobs="Age is:" . $diff. "  months";
     ?>
Subin Thomas
  • 1,408
  • 10
  • 19
-1

Simple one line code for calculating days .

$Days = round(abs(strtotime($dob)-strtotime($currentDate))/86400);
i.am
  • 23
  • 4
  • Except he was looking for months, not days. – Evert Oct 03 '15 at 09:32
  • @evert -- read this -- the above code does not show the number of days but only shows in years. please help – Igbeh Emmanuel 1 hour ago – i.am Oct 03 '15 at 09:37