[英]How Can I Calculate remain days of the year from stored Date in mysql using php
[英]How Can I calculate age from stored date birth in mysql Table using php
我有一个带有动态选择选项的表单。 我想在从数据库中选择山羊后计算从出生日期算起的几个月的年龄,并在动态选择选项后面的以下表单字段中回显所选的山羊年龄。 这是选择脚本:
<?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>';
?>
数据库表是山羊,其中“dob”作为出生日期的列,而年龄的表单字段是: <input type="text" name="age" id="age"/>
我的 PHP 代码是
<?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;
}
?>
根据您上次的评论,我给出了这个答案。
<?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";
?>
用于计算天数的简单一行代码。
$Days = round(abs(strtotime($dob)-strtotime($currentDate))/86400);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.