|
-
Aug 17th, 2010, 09:24 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Calculate the Age
How can i get my exact age? i make a code but it doesn't work. i want to happen is calculate the age. date now - date of birth then will show my age.
Code:
<?php
$day = $_POST['day']; //using select or drop down list
$month = $_POST['month']; //using select or drop down list
$year = $_POST['year']; //using select or drop down list
$age = date(("m-d-Y") - ($month.$day.$year));
echo "<h1>Your age now is $age </h1>";
?>
-
Aug 17th, 2010, 10:51 AM
#2
Re: Calculate the Age
first, use mktime() to create a Unix timestamp.
PHP Code:
$birthdate = mktime(0, 0, 0, (int) $_POST['month'], (int) $_POST['day'], (int) $_POST['year']);
then, subtract the years. if you're only interested in the year:
PHP Code:
echo "you are " . (date('Y') - date('Y', $birthdate)) . " years old.";
alternatively, if you are not storing the $birthdate variable in a database or something, you could make it even more simple:
PHP Code:
echo " you are " . (date('Y') - (int) $_POST['year']) . " years old.";
-
Aug 21st, 2010, 04:30 AM
#3
Thread Starter
Addicted Member
Re: Calculate the Age
thanks it works... but how could i use the day and month? let say my birthday is august 25 1988 and my current age is 21. when august 25 past my age will be 22? how could i code it?
-
Aug 22nd, 2010, 01:50 AM
#4
Re: Calculate the Age
the first solution in my post that makes use of mktime() will take the day and month of the year into account. the second method only deals with the year.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|