perficut Posted December 31, 2006 Share Posted December 31, 2006 Im trying to work on a small progam, playing around with numbers and that. One of the features Im trying to get accomplished is converting a variable into hh.mm format. ex.$TotalHours=(mins1+mins2);now I want to echo the $TotalMins to the screen but in a hh:mm format, so if (mins1+mins2) were equal to, say, 19.7532, then it would display 19:45(Im going to search some more and see if I can find something here already posted about this. Couldnt find anything yet) Quote Link to comment https://forums.phpfreaks.com/topic/32363-solved-convert-a-string-into-hhmm-format/ Share on other sites More sharing options...
JasonLewis Posted December 31, 2006 Share Posted December 31, 2006 not sure if there is an easier way to do it like you want it but depending on how your time comes out this should work:[code=php:0]$time = explode(".", round("19.7532", 2));echo $time[0].":".ceil(($time[1] / 100)*60);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/32363-solved-convert-a-string-into-hhmm-format/#findComment-150289 Share on other sites More sharing options...
perficut Posted December 31, 2006 Author Share Posted December 31, 2006 will give it a shot and see what happens.Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/32363-solved-convert-a-string-into-hhmm-format/#findComment-150295 Share on other sites More sharing options...
perficut Posted December 31, 2006 Author Share Posted December 31, 2006 Looks like it works. But now I have one more questions.how do I ad 25% of the tottaltime to itself?$time = explode(".", round("$totaltime", 2));echo $time[0].":".ceil(($time[1] / 100)*60); example output.First visit will take 4:00 hoursSecond visit will take 25% longer so it will echo 5:00 hoursThird visit will take 50% longer so it will echo 6:00 hoursand so on. Quote Link to comment https://forums.phpfreaks.com/topic/32363-solved-convert-a-string-into-hhmm-format/#findComment-150297 Share on other sites More sharing options...
kenrbnsn Posted December 31, 2006 Share Posted December 31, 2006 If you want to do math on time, you need to convert it to a Unix timestamp (the number of seconds since January 1, 1970).Using this format, the answer to your first question would be:[code]<?php$time = 19.7532;$x = strtotime(date('Y-m-d')) + ($time * 3600);echo date('H:i',$x);?>[/code]Here's one solution to your second problem:[code]<?php$x = 4 * 3600; // 3600 is the number of seconds in an hour$visit[1] = strtotime(date('y-m-d')) + $x;$visit[2] = strtotime(date('y-m-d')) + ($x * 1.25);$visit[3] = strtotime(date('y-m-d')) + ($x * 1.5);for ($i=1;$i<4;$i++) echo 'Visit ' . $i . ': ' . date('G:i',$visit[$i]) . ' hours<br>';?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/32363-solved-convert-a-string-into-hhmm-format/#findComment-150387 Share on other sites More sharing options...
perficut Posted January 1, 2007 Author Share Posted January 1, 2007 So far I have been able to use this routine to get what I need done. Problem Im having is that sometimes it cuts off the mins. Resulting in a display like 3:4 instead of 3:40 sometimes it display it corectly.$time = explode(".", round("19.7532", 2));echo $time[0].":".ceil(($time[1] / 100)*60); Quote Link to comment https://forums.phpfreaks.com/topic/32363-solved-convert-a-string-into-hhmm-format/#findComment-150586 Share on other sites More sharing options...
sasa Posted January 1, 2007 Share Posted January 1, 2007 try[code]<?php$t=15.05;printf("%d:%02d",floor($t),round(($t-floor($t))*60));?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/32363-solved-convert-a-string-into-hhmm-format/#findComment-150617 Share on other sites More sharing options...
perficut Posted January 1, 2007 Author Share Posted January 1, 2007 ok, looks like I've got this thing working now. Thanks everyone. Looks like I learned a little more about this coding. Quote Link to comment https://forums.phpfreaks.com/topic/32363-solved-convert-a-string-into-hhmm-format/#findComment-150639 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.