beginneratphp Posted September 14, 2007 Share Posted September 14, 2007 Hi, I have the following function: function hourstodecimal ($timeinhours) { $timeparts = explode(':', $timeinhours); return ($timeparts[0] + ($timeparts[1]/60)); } function decimaltohours ($timeindecimal) { $hours = floor($timeindecimal); $minutes = str_pad( (($timeindecimal - $hours)*60), 2, '0'); return $hours.':'.$minutes; } however, if i display 10hours and 4 (anything below 10) minutes it looks like: 10:4 and sometimes if i enter something like 10hours 43 minutes it will look like: 10:43.254221 how could i make the time to 2 decimal places like it should be and when it is below 10 it sticks a 0 infront of the minute to look correct? MANY MANY THANKS in advance! Quote Link to comment https://forums.phpfreaks.com/topic/69331-make-time-look-correct-in-php/ Share on other sites More sharing options...
phat_hip_prog Posted September 14, 2007 Share Posted September 14, 2007 what are you passing to the functions? Are you aware of the time() and date() functions? http://uk2.php.net/manual/en/function.time.php http://uk2.php.net/manual/en/function.date.php Quote Link to comment https://forums.phpfreaks.com/topic/69331-make-time-look-correct-in-php/#findComment-348368 Share on other sites More sharing options...
beginneratphp Posted September 14, 2007 Author Share Posted September 14, 2007 Passing decimal from DB thru function so it replies with a time. Quote Link to comment https://forums.phpfreaks.com/topic/69331-make-time-look-correct-in-php/#findComment-348375 Share on other sites More sharing options...
chronister Posted September 14, 2007 Share Posted September 14, 2007 I am just curious about why you want to display the time in a decimal format. Like phat_hip_prog said, check out the time(), date(), and mktime() functions. Nate Quote Link to comment https://forums.phpfreaks.com/topic/69331-make-time-look-correct-in-php/#findComment-348379 Share on other sites More sharing options...
beginneratphp Posted September 14, 2007 Author Share Posted September 14, 2007 For statistic purposes it is easier for me to keep the time in decimal format in the DB. Quote Link to comment https://forums.phpfreaks.com/topic/69331-make-time-look-correct-in-php/#findComment-348380 Share on other sites More sharing options...
phat_hip_prog Posted September 14, 2007 Share Posted September 14, 2007 unix time is easiest (e.g. time()) which is seconds since "Thu, 01 Jan 1970" (why that date I um wonder?), it's also in integer format and you can't get easier than that... anyway what do you make your decimal time from? Quote Link to comment https://forums.phpfreaks.com/topic/69331-make-time-look-correct-in-php/#findComment-348399 Share on other sites More sharing options...
zetahoff Posted September 14, 2007 Share Posted September 14, 2007 For general perpose, you can do this (obviously can be cut down to one line, but for demonstration purpose): // the time is 12:04 $h = 12; $m = 04; $str_m = $m; while (strlen($str_m)<2){$str_m = "0".$str_m;} $t = $h .":".$str_m; Quote Link to comment https://forums.phpfreaks.com/topic/69331-make-time-look-correct-in-php/#findComment-348508 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.