1

PHP 4

$a = 1359994013   (Mon, 04 Feb 2013 16:06:53)
$b = 1359997483   (Mon, 04 Feb 2013 17:04:43)

How do I calculate the difference between these two timestamps and shows the results as:

w days, x hours, y mins, z seconds.

Eg : 0 days, 0 hour, 58 mins, 10 seconds

Thanks :)

MacMan
  • 925
  • 1
  • 14
  • 32
  • 2
    What have you already tried? Hint: You do realize that both timestamps are simple integer values that you can substract from one another? – helmbert Feb 04 '13 at 17:11
  • This post should be able to help: http://stackoverflow.com/questions/5018152/php-timestamp-difference-incorrect – Lok C Feb 04 '13 at 17:13
  • 1
    PHP 4? Really? You seriously haven't upgraded your server since 2007? (would it help if I pointed out all the security flaws in PHP4?) – SDC Feb 04 '13 at 17:13
  • http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php This should work if you have PHP4 – Musk Feb 04 '13 at 17:16
  • 1
    PHP 5 includes [`DateTime::diff`](http://php.net/manual/en/datetime.diff.php), which will solve your question in a simple one-liner. But meh, if you can't upgrade then you can't upgrade. – SDC Feb 04 '13 at 17:17

2 Answers2

6

If you are into the calculation and insist on using PHP 4:

$a = 1359994013;
$b = 1359997483;

$difference = $b-$a;

$second = 1;
$minute = 60*$second;
$hour   = 60*$minute;
$day    = 24*$hour;

$ans["day"]    = floor($difference/$day);
$ans["hour"]   = floor(($difference%$day)/$hour);
$ans["minute"] = floor((($difference%$day)%$hour)/$minute);
$ans["second"] = floor(((($difference%$day)%$hour)%$minute)/$second);
echo $ans["day"] . " days, " . $ans["hour"] . " hours, "  . $ans["minute"] . " minutes, " . $ans["second"] . " seconds";

This gives you 0 days, 0 hours, 57 minutes, 50 seconds.

Antony
  • 14,900
  • 10
  • 46
  • 74
0

You can try using the date function:

http://php.net/manual/it/function.date.php