0

I am trying to find out the years, months, days and minutes between 2 timestamps.

I have used the following code, but it is not working:

$diff = time()-$res['asked_on'];
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

I am getting the output as:

0
0
0

$res['asked_on'] = 1331980897 ( It is also a time stamp getting from time() function )

sunil kumar
  • 35
  • 3
  • 8
  • 1
    What is not working? Is `$res['asked_on']` also the timestamp? – xdazz Mar 18 '12 at 06:02
  • timestamp - says db to me, most db's would have functions for this, but you don't say what your using. –  Mar 18 '12 at 06:03
  • If the diff is less than 24 hours, you'll get 0 days, since 0.9999 days will floor out to 0. – Marc B Mar 18 '12 at 06:03
  • possible duplicate of [How to get time difference in minutes in PHP](http://stackoverflow.com/questions/365191/how-to-get-time-difference-in-minutes-in-php) – Salman A Mar 18 '12 at 06:05

1 Answers1

0

If the $res['asked_on'] is a string of date format, you should turn it to timestamp by using strtotime.

And if your php >= 5.3, you could take a look at the method php provide to you: DateTime::diff

xdazz
  • 158,678
  • 38
  • 247
  • 274