实时计算年龄并将其显示在输入文本框中
real time computation of age and display it on input text box
大家好,我有一个时差公式,它在后端完美运行,但是当我 运行 页面时,输入文本框将 disappear/will 不显示在页面上。
<div class="col-md-3">
<?php
$currentdate = date("Y-m-d");
$currenttime = date("h:m:s");
$oDateNow = new DateTime($currentdate);
$oDateBirth = new DateTime($getbirthdate);
$age = $oDateNow->diff($oDateBirth);
?>
<input type="text" name="age" value="<?php echo "$age";?>" class="form-control" placeholder="Age">
</div>
当我删除公式时,输入文本框将显示。
情况是,当用户select生日时,年龄会自动显示在输入文本框上,输入文本框就在生日的正下方。
您的 $age = $oDateNow->diff($oDateBirth);
提供以下输出:
DateInterval Object ( [y] => 26 [m] => 11 [d] => 26 [h] => 0 [i] => 0 [s] => 0 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 1 [days] => 9857 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 )
如您所见,它是一个对象数组。
所以你不能echo
那个。
如果您想在 php 中找到年龄,那么您可以按照以下方式进行。
<?php
// $currentdate = date("Y-m-d");
// $currenttime = date("h:m:s");
// $oDateNow = new DateTime($currentdate);
// $oDateBirth = new DateTime($getbirthdate);
// $age = $oDateNow->diff($oDateBirth);
$bday = new DateTime($getbirthdate);
$today = new DateTime(); // use this for the current date
$diff = $today->diff($bday);
//printf('%d years, %d month, %d days', $diff->y, $diff->m, $diff->d);
?>
<input type="text" name="age" value="<?php echo "$diff->y";?>" class="form-control" placeholder="Age">
如果您想查看 JavaScript 的演示,请 click here
$age
是一个 Datetime 对象。要检索字符串版本,您需要执行类似的操作。
$age->format('%a days');
因此您的输入字段将是
<input type="text" name="age" value="<?php echo $age->format('%a days');?>" class="form-control" placeholder="Age">
Javascript
将其放在 <head></head>
元素之间:
<script type="text/javascript">
function getAge(){
var dob = document.getElementById('date').value;
dob = new Date(dob);
var today = new Date();
var age = Math.floor((today-dob) / (365.25 * 24 * 60 * 60 * 1000));
document.getElementById('age').value=age;
}
</script>
HTML
将这些放入您的 <body></body>
元素中:
Put birthdate: <input type="text" value="" id="date" name="dob" onblur="getAge();" placeholder="YYYY-MM-DD" /><br />
Your age: <input type="text" id="age" name="age" value="" class="form-control" placeholder="Age">
一旦访问者不再关注日期元素,年龄就会更新。
大家好,我有一个时差公式,它在后端完美运行,但是当我 运行 页面时,输入文本框将 disappear/will 不显示在页面上。
<div class="col-md-3">
<?php
$currentdate = date("Y-m-d");
$currenttime = date("h:m:s");
$oDateNow = new DateTime($currentdate);
$oDateBirth = new DateTime($getbirthdate);
$age = $oDateNow->diff($oDateBirth);
?>
<input type="text" name="age" value="<?php echo "$age";?>" class="form-control" placeholder="Age">
</div>
当我删除公式时,输入文本框将显示。
情况是,当用户select生日时,年龄会自动显示在输入文本框上,输入文本框就在生日的正下方。
您的 $age = $oDateNow->diff($oDateBirth);
提供以下输出:
DateInterval Object ( [y] => 26 [m] => 11 [d] => 26 [h] => 0 [i] => 0 [s] => 0 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 1 [days] => 9857 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 )
如您所见,它是一个对象数组。
所以你不能echo
那个。
如果您想在 php 中找到年龄,那么您可以按照以下方式进行。
<?php
// $currentdate = date("Y-m-d");
// $currenttime = date("h:m:s");
// $oDateNow = new DateTime($currentdate);
// $oDateBirth = new DateTime($getbirthdate);
// $age = $oDateNow->diff($oDateBirth);
$bday = new DateTime($getbirthdate);
$today = new DateTime(); // use this for the current date
$diff = $today->diff($bday);
//printf('%d years, %d month, %d days', $diff->y, $diff->m, $diff->d);
?>
<input type="text" name="age" value="<?php echo "$diff->y";?>" class="form-control" placeholder="Age">
如果您想查看 JavaScript 的演示,请 click here
$age
是一个 Datetime 对象。要检索字符串版本,您需要执行类似的操作。
$age->format('%a days');
因此您的输入字段将是
<input type="text" name="age" value="<?php echo $age->format('%a days');?>" class="form-control" placeholder="Age">
Javascript
将其放在 <head></head>
元素之间:
<script type="text/javascript">
function getAge(){
var dob = document.getElementById('date').value;
dob = new Date(dob);
var today = new Date();
var age = Math.floor((today-dob) / (365.25 * 24 * 60 * 60 * 1000));
document.getElementById('age').value=age;
}
</script>
HTML
将这些放入您的 <body></body>
元素中:
Put birthdate: <input type="text" value="" id="date" name="dob" onblur="getAge();" placeholder="YYYY-MM-DD" /><br />
Your age: <input type="text" id="age" name="age" value="" class="form-control" placeholder="Age">
一旦访问者不再关注日期元素,年龄就会更新。