我要寻找一个JavaScript或者PHP脚本,让我基于出生的他/她的日期计算某人的年龄mm/dd/yyyy
格式。 我发现这非常有帮助的链接“ 计算年龄在JavaScript ”,但它似乎是设计与用户输入的工作。 不过,我创建了一个网页,我把DOB直接进入HTML为简单的文本。 它是那么还是有可能来计算年龄? 基本上,我想这行文字是最终的结果:
29 (10/17/1983)
与值29
进行自动计算,并随着年龄的年显示。
如果这样的事情是可能的,我会为你的帮助,非常感谢。 我相当熟悉PHP和/或jQuery的,但我肯定不是一个先进的亲。
PHP:
$today = new DateTime();
$birthdate = new DateTime("1973-04-18 09:48:00");
$interval = $today->diff($birthdate);
echo $interval->format('%y years');
看到它在行动 。 可以很明显的格式化出来,以满足您的需求。
在javascript中你可以计算出它:
function getAge(dateString) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
计算年龄的最简单方法是:
CURRENT YEAR - BIRTH YEAR = AGE
IF(CURRENT MONTH DAY < BIRTH MONTH DAY) AGE--;
IE年还活着,但-1,如果您还没有一个生日还
月日是0301 3月1日
使用Javascript:
这可能不会超过你所要求,但会给你的年,月,日和年龄。
是这样的: 8 years, 7 months, 21 days old
这种运作良好,如果你有一个网站只有几天大新生。
这事近9年前,我在我女儿的网站使用
function GetMyAge()
{
<!--
birthTime = new Date("July 25, 2004 00:00:00 GMT-0600")
todaysTime = new Date();
<!-- Parse out specific date values
todaysYear = todaysTime.getFullYear()
todaysMonth = todaysTime.getMonth()
todaysDate = todaysTime.getDate()
todaysHour = todaysTime.getHours()
todaysMinute = todaysTime.getMinutes()
todaysSecond = todaysTime.getSeconds()
birthYear = birthTime.getFullYear()
birthMonth = birthTime.getMonth()
birthDate = birthTime.getDate()
birthHour = birthTime.getHours()
birthMinute = birthTime.getMinutes()
birthSecond = birthTime.getSeconds()
<!-- Adjusts for Leap Year Info
if ((todaysYear / 4) == (Math.round(todaysYear / 4))) {
countLeap = 29}
else {
countLeap = 28}
<!-- Calculate the days in the month
if (todaysMonth == 2) {
countMonth = countLeap}
else {
if (todaysMonth == 4) {
countMonth = 30}
else {
if (todaysMonth == 6) {
countMonth = 30}
else {
if (todaysMonth == 9) {
countMonth = 30}
else {
if (todaysMonth == 11) {
countMonth = 30}
else {
countMonth = 31}}}}}
<!-- Doing the subtactions
if (todaysMinute > birthMinute) {
diffMinute = todaysMinute - birthMinute
calcHour = 0}
else {
diffMinute = todaysMinute + 60 - birthMinute
calcHour = -1}
if (todaysHour > birthHour) {
diffHour = todaysHour - birthHour + calcHour
calcDate = 0}
else {
diffHour = todaysHour + 24 - birthHour + calcHour
calcDate = -1}
if (todaysDate > birthDate) {
diffDate = todaysDate - birthDate + calcDate
calcMonth = 0}
else {
diffDate = todaysDate + countMonth - birthDate + calcDate
calcMonth = -1}
if (todaysMonth > birthMonth) {
diffMonth = todaysMonth - birthMonth + calcMonth
calcYear = 0}
else {
diffMonth = todaysMonth + 12 - birthMonth + calcMonth
calcYear = -1}
diffYear = todaysYear - birthYear + calcYear
<!-- Making sure it all adds up correctly
if (diffMinute == 60) {
diffMinute = 0
diffHour = diffHour + 1}
if (diffHour == 24) {
diffHour = 0
diffDate = diffDate + 1}
if (diffDate == countMonth) {
diffDate = 0
diffMonth = diffMonth + 1}
if (diffMonth == 12) {
diffMonth = 0
diffYear = diffYear + 1}
if (diffYear != 1)
YearPlural = "s"
else
YearPlural=""
if (diffMonth != 1)
MonthPlural = "s"
else
MonthPlural=""
if (diffDate != 1)
DatePlural = "s"
else
DatePlural=""
if (diffYear == 0 && diffMonth == 0)
return (diffDate + ' day' + DatePlural + ' old. ');
else if (diffYear == 0)
return (diffMonth + ' month' + MonthPlural + ', ' + diffDate + ' day' + DatePlural + ' old. ');
else
return (diffYear + ' year' + YearPlural + ', ' + diffMonth + ' month' + MonthPlural + ', ' + diffDate + ' day' + DatePlural + ' old. ');
}
// -->
文章来源: Calculate age (jQuery or PHP) based on date of birth [not based on user input]