Page 1 of 1

Conversions: time2number format and number2time format

Posted: Thu Sep 25, 2008 5:08 am
by sifar786
Hi all,

does anyone know how to convert time format to numeric format & vice versa?

i have made a function to which i am passing a time variable:

function time2number($t)
{
if(strpos($t,':') && !strpos($t,'.'))
{
$datetime = str_replace(':', ' ', $t);
$array = explode(' ', $datetime);
$hour = $array[0];
$minute = $array[1]/60;
$second = $array[2]/3600;
$minsec = $minute+$second;

echo "time: ".$hour." ".$minsec."<BR/>";
if($minsec >= 0.60)
{
$hour += 1;
$minsec = $minsec - 0.60;
}
return ($hour+$minsec);
}

else

{
if(strpos($datetime,'.'))
{
$datetime = str_replace('.', ' ', $t);
}
$array = explode(' ', $datetime);
$hour = $array[0];
$minsec = $array[1];

echo "numeric: ".$hour." ".$minsec."<BR/>";

if($minsec >= 60)
{
$hour += 1;
$minsec = $minsec - 60;
}
return ($hour+$minsec);
}
}

If i pass a Time format variable e.g. 10:25:33, the 1st part of IF works! But if i pass a numeric time format e.g. 229100 seconds, then ideally it should go to the ELSE part and then convert it to correct number format, but it fails!

Also, if numeric time is like this e.g. 10, then how to check if its a time format or number format in the function?


"select sum(( unix_timestamp( tst.end_time ) - unix_timestamp( tst.start_time ))/3600) from TIMES_TABLE"

the above statement is a part of a query n i think gives me the time like this: 4.75 etc, which i want to convert into numeric hours & minutes e.g. 5.15 & not 4.75 (1 hr= 60 mins)


Regards

Re: Conversions: time2number format and number2time format

Posted: Thu Sep 25, 2008 7:46 am
by Jenk
strtotime() ?