Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate year from entering my age using php

Tags:

date

php

How can i calculate year from entering my age using php.

example:

i am entering my age as 24.

So that i need to get the year as 1985.

how this can be done.

thanks in advance

like image 293
Fero Avatar asked Jun 16 '10 05:06

Fero


People also ask

How do you calculate age per year?

Age of a Person = Given date - Date of birth. Ron's Date of Birth = July 25, 1985. Given date = January 28, 2021. Years' Difference = 2020 - 1985 = 35 years.

How do I get year from age in SQL?

Using SQL's DateDiff() for Age Most likely, age at the time of transaction isn't a column already in your data as it is dependent on when a certain event occurs. The short solution is to use the built-in function DATEDIFF( ) where you are able to find the year difference between two dates.


2 Answers

If you knew simple math, you'd know that 2010 - 24 - 1 = 1985 or $year = date("Y") - $theirage - 1... But just giving an age does not accurately deduce what year they were born. For example:

If the date is December 30, 2010 and they say they're 24, you're still saying they were born in 1985 when chances are very, very high that they were actually born in 1986. You cannot rely on their age to give you their birth year.

EDIT If that wasn't very clear:

Today's date is June 16, 2010. So to be 24 years old, I would have needed to be born somewhere between June 17, 1985 and June 16, 1986. That's near half of the birthyears that would be 1985 and near half that would be 1986, causing a very high inaccuracy.

like image 114
animuson Avatar answered Oct 19 '22 22:10

animuson


To obtain the date 24 years ago, you can use strtotime:

$date = strtotime("-24 year");
echo date('%c', $date);
like image 35
Sjoerd Avatar answered Oct 19 '22 22:10

Sjoerd