sifar786 Posted September 25, 2008 Share Posted September 25, 2008 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: <?php 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 Link to comment https://forums.phpfreaks.com/topic/125769-conversions-time2number-format-and-number2time-format/ Share on other sites More sharing options...
kenrbnsn Posted September 25, 2008 Share Posted September 25, 2008 What's wrong with using the built-in functions strtotime() and date()? Ken Link to comment https://forums.phpfreaks.com/topic/125769-conversions-time2number-format-and-number2time-format/#findComment-650381 Share on other sites More sharing options...
sifar786 Posted September 25, 2008 Author Share Posted September 25, 2008 i am new to PHP. can you show me an example using both the functions wherein i can convert from time to numeric format? regards. Link to comment https://forums.phpfreaks.com/topic/125769-conversions-time2number-format-and-number2time-format/#findComment-650417 Share on other sites More sharing options...
MatthewJ Posted September 25, 2008 Share Posted September 25, 2008 <?php $timevar = '2007-10-02 13:29:17' $timevar = strtotime($timevar); echo $timevar; ?> Link to comment https://forums.phpfreaks.com/topic/125769-conversions-time2number-format-and-number2time-format/#findComment-650472 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.