Figured I'd throw this on here since this seems to be most popular form of this question.
I ran a 100 year comparison on 3 of the most popular types of age funcs i could find for PHP and posted my results [as well as the functions] to my blog.
As you can see there, all 3 funcs preform well with just a slight difference on the 2nd function. My suggestion based on my results is to use the 3rd function unless you want to do something specific on a person's birthday, in which case the 1st function provides a simple way to do exactly that.
Found small issue with test, and another issue with 2nd method! Update coming to blog soon! For now, I'd take note, 2nd method is still most popular one I find online, and yet still the one I'm finding the most inaccuracies with!
My suggestions after my 100 year review:
If you want something more elongated so that you can include occasions like birthdays and such:
function getAge[$date] { // Y-m-d format
$now = explode["-", date['Y-m-d']];
$dob = explode["-", $date];
$dif = $now[0] - $dob[0];
if [$dob[1] > $now[1]] { // birthday month has not hit this year
$dif -= 1;
}
elseif [$dob[1] == $now[1]] { // birthday month is this month, check day
if [$dob[2] > $now[2]] {
$dif -= 1;
}
elseif [$dob[2] == $now[2]] { // Happy Birthday!
$dif = $dif." Happy Birthday!";
};
};
return $dif;
}
getAge['1980-02-29'];
But if you just simply want to know the age and nothing more, then:
function getAge[$date] { // Y-m-d format
return intval[substr[date['Ymd'] - date['Ymd', strtotime[$date]], 0, -4]];
}
getAge['1980-02-29'];
See BLOG
A key note about the strtotime
method:
Note:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the
separator between the various components: if the separator is a slash [/],
then the American m/d/y is assumed; whereas if the separator is a dash [-]
or a dot [.], then the European d-m-y format is assumed. If, however, the
year is given in a two digit format and the separator is a dash [-, the date
string is parsed as y-m-d.
To avoid potential ambiguity, it's best to use ISO 8601 [YYYY-MM-DD] dates or
DateTime::createFromFormat[] when possible.
Some web applications are needed to show the age of the user. In that case, you need to calculate the user age from date of birth. Here we’ll provide a short PHP code snippet to calculate age from date of birth.
In the following code, date[]
, date_create[]
, and date_diff[]
functions are used to calculate age of the user till today in PHP.
$dateOfBirth = "17-10-1985";
$today = date["Y-m-d"];
$diff = date_diff[date_create[$dateOfBirth], date_create[$today]];
echo 'Age is '.$diff->format['%y'];
Use the above code to get the age from date of birth using PHP date functions.
Write a PHP script to calculate the current age of a person. Sample date of birth : 11.4.1987 Sample Solution:PHP date: Exercise-18 with Solution
PHP Code:
Sample Output:
Your age : 30 years, 3 month, 0 days
N.B.: The result may varry for your system date and time.
Flowchart :
PHP Code Editor:
Have another way to solve this solution? Contribute your code [and comments] through Disqus.
Previous: Write a PHP function to
get start and end date of a week [by week number] of a particular year.
Next: Write a PHP script to calculate weeks between two dates.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
PHP: Tips of the Day
PHP - how to create a newline character?
Only double quoted strings interpret the escape sequences \r and \n as '0x0D' and '0x0A' respectively, so you want:
"\r\n"
Single quoted strings, on the other hand, only know the escape sequences \\ and \'.
So unless you concatenate the single quoted string with a line break generated elsewhere [e. g., using double quoted string "\r\n" or using chr function chr[0x0D].chr[0x0A]], the only other way to have a line break within a single quoted string is to literally type it with your editor:
$s = 'some text before the line break some text after';
Make sure to check your editor for its line break settings if you require some specific character sequence [\r\n for example].
Ref : //bit.ly/3hcyege
- Exercises: Weekly Top 16 Most Popular Topics
- SQL Exercises, Practice, Solution - JOINS
- SQL Exercises, Practice, Solution - SUBQUERIES
- JavaScript basic - Exercises, Practice, Solution
- Java Array: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : Conditional Statement
- HR Database - SORT FILTER: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : String
- Python Data Types: Dictionary - Exercises, Practice, Solution
- Python Programming Puzzles - Exercises, Practice, Solution
- C++ Array: Exercises, Practice, Solution
- JavaScript conditional statements and loops - Exercises, Practice, Solution
- C# Sharp Basic Algorithm: Exercises, Practice, Solution
- Python Lambda - Exercises, Practice, Solution
- Python Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools
- JavaScript: HTML Form Validation