-1

I have a function which calculates age from a given date

public function showAge()
    {
        if ($this->account_id == 1 && !empty($this->profile->birthday)) 
        {
            return date_diff(date_create($this->profile->birthday), date_create('today'))->y;
        }
    }

My question is: can I do some kind of reverse function (calculate the date by a given age)?

Web Student
  • 735
  • 3
  • 15
  • 27
  • possible duplicate of [How to calculate the difference between two dates using PHP?](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Sterling Archer Sep 02 '14 at 16:18
  • Unless this is the user's birthday for sure, you can not calculate the exact date from the current date... Let's say my birthday is 28th of August, and I'm 26, then you can get my birthday, but if I do not provide you my day/month of birth you only get my year of birth. – Thomas Dutrion Sep 02 '14 at 16:19
  • Not a possible duplicate, because i dont need difference between 2 dates, i need date based on AGE – Web Student Sep 02 '14 at 16:20
  • @WebStudent easy to calculate if you subtract the two dates. The year subtraction result is the age. – Sterling Archer Sep 02 '14 at 16:21
  • my bad than, thank you @SterlingArcher , will take a look – Web Student Sep 02 '14 at 16:22
  • You could know the year someone was born if you know their age and whether or not they've already had a birthday this year. That's about as close as you can get. – p.s.w.g Sep 02 '14 at 16:23

2 Answers2

2

Date => Age is a many-to-one mapping, because for each of the 365 days that I am 22 years old... I am 22 years old.

However, just by knowing that I am 22 years old does not let you infer the date.

A similar question would be "knowing that 4 + 6 = 10, can you get 4 and 6 from 10?" to which the answer is no, because 5 + 5 is also 10. In this way, addition of integers would also be a many-to-one mapping.

They cannot be reversed.

That said, if you know their date of birth, then you can simply add the age to the year of birth and get the date of their most recent birthday.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1
<?php
$age = 26;
echo date('Y') - $age;

That's the best you can get unless the current day is fortunately the day/month of birth of the user...

Thomas Dutrion
  • 1,844
  • 1
  • 11
  • 9