tags:

views:

99

answers:

4

I Have to calculate date time difference how to do that in PHP. I need exact hours , mins and secs. any body have the scripts for that please give me :-P

A: 

The datediff function at addedbytes.com lets you to just that easily :)

Sarfraz
A: 

Check this.. This should work

<?php

function timeDiff($firstTime,$lastTime)
{

// convert to unix timestamps
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);

// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;

// return the difference
return $timeDiff;
}

//Usage :
echo timeDiff("2002-04-16 10:00:00","2002-03-16 18:56:32");

?> 
Vimard
Hi vimard thanks for your valuable replay. Actually I want to display the oldness of files like in facebook or orkut. they shows time, days, week, year based display.
php learner
so this solves your problem rite...
Vimard
A: 

For something like that use the built int time() function.

  • Store the value of time(); something like 1274467343 which is the number of seconds scince
  • When ever needed retrive the value and assign it to $erlierTime.
  • Assign time() again to the latter stop time you would want, lets just call it $latterTime

So now you have something like $erlierTime and $latterTime.

No just do $latterTime - $erlierTime to get the difference in seconds and then do your divisions to get numb of minutes passed, num of hours passed etc.


In order for me or any one to give you a complete script you would need to tell us what your environment is like and what are you working with mysql, sqlite etc... also would need to know how that timer is triggered.

Babiker
A: 

Not sure if I understand your question right, but if you are looking for some kind of stop watch, you could use following class:

class StopWatch {
    private static $total;

    public static function start() {
        self::$total  = microtime(true);
    }

    public static function elapsed() {
        return microtime(true) - self::$total;
    }
}

I use it for checking how long does it take to load a page. But it should give you general idea how to implement it basically anywhere. The difference is in seconds in this case, but changing that into proper time stamp shouldn't be difficult.

Ondrej Slinták