will35010 Posted November 30, 2009 Share Posted November 30, 2009 I have this function that I want to calculate the time difference. I want it to output in the format: HH:MM. Where do I go from where I'm at? Thanks!!! function getTIME($time) { ## example start/end dates $startdate = $time; $enddate = time(); ## difference between the two in seconds $time_period = ( $enddate - $startdate ); Quote Link to comment https://forums.phpfreaks.com/topic/183493-need-help-with-function-to-calculate-time/ Share on other sites More sharing options...
taquitosensei Posted November 30, 2009 Share Posted November 30, 2009 this should be close $minutes=floor($time_period/60); $hours=floor($minutes/60); if($hours==1) { $hoursdisplay=$hours." Hour "; } if(($minutes%60) > 1) { $minutesdisplay=($minutes%60)." Minutes"; else if(($minutes%60)==1) { $minutesdisplay=($minutes%60)." Minute"; else { $minutesdisplay=""; } $display=$hoursdisplay.$minutesdisplay; Quote Link to comment https://forums.phpfreaks.com/topic/183493-need-help-with-function-to-calculate-time/#findComment-968567 Share on other sites More sharing options...
will35010 Posted November 30, 2009 Author Share Posted November 30, 2009 Ok. I got this far. It's displaying the total minutes, so what am I missing on my math? Also, is there a way to make it show only the whole number and leave off everything past the decimal point? Thanks. function getTIME($time) { ## example start/end dates $startdate = $time; $enddate = time(); ## difference between the two in seconds $time_period = ( $enddate - $startdate ); $day = '86400'; $hour = '3600'; $minute = '60'; $second = '1'; $days = ($time_period / $day); $hours = ($time_period / $hour); $minutes = ($time_period / $minute); $seconds = ($time_period / $second); echo $hours.":".$minutes; } Quote Link to comment https://forums.phpfreaks.com/topic/183493-need-help-with-function-to-calculate-time/#findComment-968570 Share on other sites More sharing options...
will35010 Posted November 30, 2009 Author Share Posted November 30, 2009 I fixed the displaying past the decimal, but I cannot get it to display right. It's displaying like this: 21:1,273 function getTIME($time) { ## example start/end dates $startdate = $time; $enddate = time(); ## difference between the two in seconds $time_period = ( $enddate - $startdate ); $day = '86400'; $hour = '3600'; $minute = '60'; $second = '1'; $days = ($time_period / $day); $hours = ($time_period / $hour); $minutes = ($time_period / $minute); $seconds = ($time_period / $second); echo number_format($hours).":".number_format($minutes); } Quote Link to comment https://forums.phpfreaks.com/topic/183493-need-help-with-function-to-calculate-time/#findComment-968581 Share on other sites More sharing options...
DavidAM Posted December 1, 2009 Share Posted December 1, 2009 You could use the suggestion taquitosensei gave you. ($minutes % 60) will return the number of minutes (between 0 and 59) with the hours, days, etc. stripped off. But it you want all of the components (which you don't seem to be using) you have to subtract out each level as you go through: function getTIME($time) { ## example start/end dates $startdate = $time; $enddate = time(); ## difference between the two in seconds $time_period = ( $enddate - $startdate ); $day = '86400'; $hour = '3600'; $minute = '60'; $second = '1'; $days = intval($time_period / $day); $time_period -= ($days * $day); $hours = ($time_period / $hour); $time_period -= ($hours * $hour); $minutes = ($time_period / $minute); $time_period -= ($minutes * $minute); $seconds = ($time_period / $second); echo number_format($hours).":".number_format($minutes); } and I wouldn't use number_format, instead use: printf('%2d:%02d, $hours, $minutes) or, the quickest way, might be echo date($time_period, 'H:i'); I think that would work, haven't tried it. Quote Link to comment https://forums.phpfreaks.com/topic/183493-need-help-with-function-to-calculate-time/#findComment-968590 Share on other sites More sharing options...
will35010 Posted December 1, 2009 Author Share Posted December 1, 2009 I'm using this code now, but it keeps giving me a parse T_ELSE error for this line: else if(($minutes%60)==1) { $minutesdisplay=($minutes%60)." Minute"; function getTIME($time) { ## example start/end dates $startdate = $time; $enddate = time(); ## difference between the two in seconds $time_period = ( $enddate - $startdate ); $minutes=floor($time_period/60); $hours=floor($minutes/60); if($hours==1) { $hoursdisplay=$hours." Hour "; } if(($minutes%60) > 1) { $minutesdisplay=($minutes%60)." Minutes"; else if(($minutes%60)==1) { $minutesdisplay=($minutes%60)." Minute"; else { $minutesdisplay=""; } $display=$hoursdisplay.$minutesdisplay; echo $display; } Quote Link to comment https://forums.phpfreaks.com/topic/183493-need-help-with-function-to-calculate-time/#findComment-968602 Share on other sites More sharing options...
taquitosensei Posted December 1, 2009 Share Posted December 1, 2009 I think it's because I made a typo and forgot the closing bracket Quote Link to comment https://forums.phpfreaks.com/topic/183493-need-help-with-function-to-calculate-time/#findComment-968603 Share on other sites More sharing options...
will35010 Posted December 1, 2009 Author Share Posted December 1, 2009 I think it's because I made a typo and forgot the closing bracket Ok. Thanks for your help! I got it working with this, but it's not showing the hours. Did I add too many brackets? function getTIME($time) { ## example start/end dates $startdate = $time; $enddate = time(); ## difference between the two in seconds $time_period = ( $enddate - $startdate ); $minutes=floor($time_period/60); $hours=floor($minutes/60); if($hours==1) { $hoursdisplay=$hours." Hour "; } if(($minutes%60) > 1) { $minutesdisplay=($minutes%60)." Minutes"; } else if(($minutes%60)==1) { $minutesdisplay=($minutes%60)." Minute"; } else { $minutesdisplay=""; } $display=$hoursdisplay.$minutesdisplay; echo $display; } Quote Link to comment https://forums.phpfreaks.com/topic/183493-need-help-with-function-to-calculate-time/#findComment-968605 Share on other sites More sharing options...
will35010 Posted December 1, 2009 Author Share Posted December 1, 2009 Fixed it by using this. Thanks for everybody's help! function getTIME($time) { //start/end dates $startdate = $time; $enddate = time(); //difference between the two in seconds $time_period = ( $enddate - $startdate ); $minutes=floor($time_period/60); $hours=floor($minutes/60); if($hours==1) { $hoursdisplay=$hours; } if(($minutes%60) > 1) { $minutesdisplay=($minutes%60); } else if(($minutes%60)==1) { $minutesdisplay=($minutes%60); } else { $minutesdisplay=""; } $display=$hours.":".$minutesdisplay; echo $display; } Quote Link to comment https://forums.phpfreaks.com/topic/183493-need-help-with-function-to-calculate-time/#findComment-968609 Share on other sites More sharing options...
taquitosensei Posted December 1, 2009 Share Posted December 1, 2009 I guess I didn't read the original post very well. If you're not going to do the words Hours and Minutes you can make it even simpler. $minutes=floor($time_period/60); $hours=floor($minutes/60); echo $hours.":".($minutes%60); Quote Link to comment https://forums.phpfreaks.com/topic/183493-need-help-with-function-to-calculate-time/#findComment-968610 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.