[英]How to calculate age from given data on input field?
I have searched but i didn't get perfect solution of my issue.I can calculate age in normal way.But i haven't any idea how can i calculate age from given data on input field.I have used jQuery DatePicker for input field.My code: 我已经搜索过,但是我没有完美解决问题的方法。我可以按正常方式计算年龄。但是我不知道如何从输入字段中的给定数据中计算年龄。我已经使用jQuery DatePicker作为输入字段我的代码:
$birthday = new DateTime('1970-02-01');
$to = new DateTime('today');
echo $birthday ->diff($to)->y.' Years, '.$birthday ->diff($to)->m.' Months, '.$birthday ->diff($to)->d.' Days ';
This is my field: 这是我的领域:
Now i want $birthday
will be dynamic and it will insert and want to show immediately age calculation without page load. 现在我希望
$birthday
是动态的,它将插入并希望立即显示年龄计算而无需页面加载。 Have any clue? 有什么线索吗? Thanks in advance.
提前致谢。
The jQuery datepicker displays the dates in an input field with id="datepicker"
that you can use to get your date. jQuery datepicker在输入字段中显示日期,其中
id="datepicker"
可用于获取日期。 For a client-side calculation, the momentjs
library mentionned in other answer if perfect for this job. 对于客户端计算,其他答案中提到的
momentjs
库是否适合此工作。
fromdate = $('#datepicker').val(); console.log(fromdate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text" id="datepicker" class="hasDatepicker" value="2016/14/12">
If you want to calculate server-side, you can pass the value to PHP without reloading page with Ajax: 如果要计算服务器端,可以将该值传递给PHP,而无需使用Ajax重新加载页面:
$('#datepicker').change(function(){
$.ajax({
type: "POST",
url: "datecalc.php",
data: {text:$(this).val()}
});
});
If you want to calculate the age in jQuery, you can use moment.js
like this: 如果要在jQuery中计算年龄,则可以像下面这样使用
moment.js
:
moment("05/05/1998", "MM/DD/YYYY").month(0).from(moment().month(0));
// Output = 22 years ago
$(function() { var dob = $('.dob').val(); $('.dob').on('input', function(e) { var a = moment($(this).val(), "MM/DD/YYYY").month(0).from(moment().month(0)) console.log(a); }); var a = moment($('.dob').val(), "MM/DD/YYYY").month(0).from(moment().month(0)) console.log(a); })
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class='dob' type='text' value="05/05/1996">
Inside your controller, you can use Carbon's age
method like this: 在控制器内部,您可以使用Carbon的
age
方法,如下所示:
$birthday = Carbon\Carbon::parse('1970-02-01');
$age = $birthday->age;
// Output - (int) 46
See Carbon Docs for reference
请参阅Carbon Docs以供参考
Hope this helps! 希望这可以帮助!
You need to read the value of the data input field using JQuery or Javascript and then pass the value to your age calculation function. 您需要使用JQuery或Javascript读取数据输入字段的值,然后将该值传递给年龄计算函数。
For example if your date field has id="date-picker", then you can read the date as $("date-picker").val() or document.getElementById("date-picker").value() 例如,如果您的日期字段具有id =“ date-picker”,则可以将日期读取为$(“ date-picker”)。val()或document.getElementById(“ date-picker”)。value()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.