Friday, September 25, 2009

Get the difference between time in Hours, Minutes, Seconds.

[php]
class timedifference
{
var $fromHours;
var $fromMinutes;
var $fromSeconds;
var $toHours;
var $toMinutes;
var $toSeconds;
var $Hrs;
var $Mnts;
var $Secs;
function HrsMinsSecs($fromTime,$toTime)
{
list($this->fromHours,$this->fromMinutes,$this->fromSeconds ) = explode(":",$fromTime) ;
list($this->toHours,$this->toMinutes,$this->toSeconds ) = explode(":",$toTime) ;
if($this->toHours < $this->fromHours)
return "Invalid from and to times";
$this->findSeconds();
$this->findMinutes();
$this->findHours();
return "Hours : ".$this->Hrs." Minutes : ".$this->Mnts." Seconds : ".$this->Secs;

}
function findSeconds()
{
if($this->toSeconds < $this->fromSeconds)

{
$this->Secs = ($this->toSeconds + 60) - $this->fromSeconds;
$this->toMinutes--;

}
else

{
$this->Secs = $this->toSeconds - $this->fromSeconds;
}
}
function findMinutes()
{
if($this->toMinutes < $this->fromMinutes)

{
$this->Mnts = ($this->toMinutes + 60) - $this->fromMinutes;
$this->toHours--;
}
else
{
$this->Mnts = $this->toMinutes - $this->fromMinutes;
}
}
function findHours()

{
$this->Hrs = $this->toHours - $this->fromHours;
}

} // end of class
$timeData = new timedifference();

echo $timeData->HrsMinsSecs("08:33:13","18:01:59");
[/]