php - calculate goat age from its date birth and echo in a field on form -
i have form dynamic select option. want calculate age of goat in months date of birth after selecting db , echo selected goat age on following form field follows dynamic selection option. here selection script:
<?php $conn = mysqli_connect("localhost", "root", "xxx", "xxxx"); if(mysqli_connect_errno($conn)) { echo "unable connect database server"; } $sql = "select * goats sex='male'"; $query = mysqli_query($conn, $sql); echo '<select name="hegoat">'; echo '<option value="">choose goat</option>'; while($hegoats = mysqli_fetch_assoc($query)){ echo "<option>{$hegoats['goatid']}</option>"; } echo '</select>'; ?>
data base table goats 'dob' column date of birth while form field age is:<input type="text" name="age" id="age"/>
and php code
<?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; } ?>
as per last comment, giving 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 , month difference $dobs="age is:" . $diff. " months"; ?>
Comments
Post a Comment