Conversions: time2number format and number2time format

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sifar786
Forum Commoner
Posts: 27
Joined: Thu Aug 21, 2008 11:50 pm

Conversions: time2number format and number2time format

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Conversions: time2number format and number2time format

Post by Jenk »

strtotime() ?
Post Reply