0

I want to get the age of users. I tried:

$dStart = strtotime('1985-10-24');
    $dEnd   = strtotime('2014-07-30');
    $dDiff  = $dEnd - $dStart;
    echo date('Y',$dDiff);

But get 1998 instead 28, what would be the right code?

John Conde
  • 217,595
  • 99
  • 455
  • 496
user3838972
  • 331
  • 1
  • 5
  • 10
  • Think about what you're actually calculating with *timestamp - timestamp*. The result would be the number of seconds between the two dates which is itself, not a date – Phil Jul 29 '14 at 23:37
  • I got this code from a question here and it was marked as accepted. I knew that couldn't be right but I can't think of something else – user3838972 Jul 29 '14 at 23:40
  • I got it from here: http://stackoverflow.com/questions/15743882/php-date-difference ok that wasn't accepted but voted up – user3838972 Jul 29 '14 at 23:44

1 Answers1

4

strtotime() is not made for date math. DateTime() is much better suited for this:

$dStart = new DateTime('1985-10-24');
$dEnd = new DateTime('2014-07-30');
$dDiff = $dStart->diff($dEnd);
echo $dDiff->y;

Demo

John Conde
  • 217,595
  • 99
  • 455
  • 496