How to calculate year from my age using php

How can I calculate a year from my age using php.

Example:

I enter my age at the age of 24.

So, I need to get a year in 1985.

How can I do that.

early

+5
source share
7 answers

If you knew simple math, you would know that 2010 - 24 - 1 = 1985or $year = date("Y") - $theirage - 1... But it’s just that age doesn’t exactly determine what year they were born. For instance:

30 2010 , , 24, , 1985 , , , 1986 , .

:

- 16 2010 . , 24 , - 17 1985 16 1986 . , 1985 , 1986 , .

+7

24 , strtotime:

$date = strtotime("-24 year");
echo date('%c', $date);
+1

DateTime:

PHP > 5.2

<?php
       function getBirthYear($age) {
               $now = new DateTime();
               $now->modify("-" . $age . " years");
               return $now->format("Y");
       }

       echo getBirthYear(24);
?>

PHP > 5.3

<?php
       function getBirthYear($age) {
               $now = new DateTime();
               $now->sub(new DateInterval("P" . $age . "Y"));
               return $now->format("Y");
       }

       echo getBirthYear(24);
?>
+1

, .

If you are 24 years old today (we usually round, not round), you were born between June 17, 1985 and June 16, 1986.

Methods for adding and subtracting dates are already posted on this page.

+1
source

I use a simple php script that I made myself: My_Age_Php

+1
source

PHP 5.3.0 also implemented the date_difffunction as part of the DateTime class.

0
source

WARNING: This is unsure, but its an attempt.

$yearBorn = round((((time()-($age*31556926))/31556926)+70));
$yearBorn = ($yearBorn>100)? "20".substr($yearBorn,1,2):"19".$yearBorn;
0
source

All Articles