根据出生日期[不基于用户输入]计算年龄(jQuery或PHP) [英] Calculate age (jQuery or PHP) based on date of birth [not based on user input]

查看:115
本文介绍了根据出生日期[不基于用户输入]计算年龄(jQuery或PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个JavaScript或PHP脚本,允许我根据他/她的出生日期以 mm / dd / yyyy 格式计算某人的年龄。我发现这个非常有用的链接用JavaScript计算年龄但它似乎是为了工作而设计的用户输入。但是,我正在创建一个页面,我将DOB直接放入HTML作为简单文本。那么还可以计算年龄吗?基本上,我希望这行文字是最终结果:

I am looking for a JavaScript or PHP script that allows me to calculate somebody's age based on his/her date of birth in the mm/dd/yyyy format. I found this very helpful link "Calculate age in JavaScript" but it seems to be designed to work with user input. However, I am creating a page where I put the DOB directly into the HTML as simple text. Is it then still possible to calculate the age? Basically, I want this line of text to be the end-result:

29 (10/17/1983)

值为 29 自动计算并显示为多年的年龄。

with the value of 29 being automatically computed and displayed as the age in years.

如果这样的事情可能发生,我将非常感谢你的帮助。我对PHP和/或jQuery非常熟悉,但我当然不是高级专家。

If something like this is possible, I would be very grateful for your help. I am fairly familiar with PHP and/or jQuery but I'm certainly not an advanced pro.

推荐答案

PHP:

$today = new DateTime();
$birthdate = new DateTime("1973-04-18 09:48:00");
$interval = $today->diff($birthdate);
echo $interval->format('%y years');

在行动中看到它。显然,您可以根据自己的需要对其进行格式化。

See it in action. You can obviously format the out to suit your needs.

这篇关于根据出生日期[不基于用户输入]计算年龄(jQuery或PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆