etrader Posted September 8, 2011 Share Posted September 8, 2011 Other than if statement, is there a way to change a duration give in seconds (e.g. 5746 seconds) to the standard format of x hours and y minutes and z seconds? Quote Link to comment https://forums.phpfreaks.com/topic/246686-changing-duration-in-seconds-to-hours-and-minutes/ Share on other sites More sharing options...
voip03 Posted September 8, 2011 Share Posted September 8, 2011 Conditional Statements Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. In PHP we have the following conditional statements: •if statement - use this statement to execute some code only if a specified condition is true •if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false •if...elseif....else statement - use this statement to select one of several blocks of code to be executed •switch statement - use this statement to select one of many blocks of code to be executed Quote Link to comment https://forums.phpfreaks.com/topic/246686-changing-duration-in-seconds-to-hours-and-minutes/#findComment-1266733 Share on other sites More sharing options...
Nodral Posted September 8, 2011 Share Posted September 8, 2011 Probably the best approach is to create a function to do this, then you can reuse whenever you need. I have a couple which I have built to convert unix timestamps to HH:MM:SS or DD:MM:YY as required. The date/time fuctions within MySQL may also be handy if you're interacting with a DB. If you read the php & MySQL manauls, you'll find loads of options Quote Link to comment https://forums.phpfreaks.com/topic/246686-changing-duration-in-seconds-to-hours-and-minutes/#findComment-1266735 Share on other sites More sharing options...
funstein Posted September 8, 2011 Share Posted September 8, 2011 You could use the division(/) and operator like this : function convertSecondsToMinutes($amountofseconds){ $minutes = $amountofseconds / 60 ; $minutes = floor($minutes) ; $seconds = $minutes * 60 ; $seconds = $amountofseconds - $seconds ; $return = "$minutes minutes and $seconds seconds." ; return $return ; } Wanna test it ? Visit the link : http://codepad.org/IXHMDqNr Quote Link to comment https://forums.phpfreaks.com/topic/246686-changing-duration-in-seconds-to-hours-and-minutes/#findComment-1266774 Share on other sites More sharing options...
etrader Posted September 8, 2011 Author Share Posted September 8, 2011 Thanks for the idea, funstein I wrote a function to consider hour too. Thank you all fellas Quote Link to comment https://forums.phpfreaks.com/topic/246686-changing-duration-in-seconds-to-hours-and-minutes/#findComment-1266777 Share on other sites More sharing options...
funstein Posted September 8, 2011 Share Posted September 8, 2011 No problem, glad I could help Quote Link to comment https://forums.phpfreaks.com/topic/246686-changing-duration-in-seconds-to-hours-and-minutes/#findComment-1266784 Share on other sites More sharing options...
funstein Posted September 8, 2011 Share Posted September 8, 2011 Btw, mind PM'ing me your function? Quote Link to comment https://forums.phpfreaks.com/topic/246686-changing-duration-in-seconds-to-hours-and-minutes/#findComment-1266786 Share on other sites More sharing options...
etrader Posted September 8, 2011 Author Share Posted September 8, 2011 Here, it is function duration($amount){ $h = $amount / 3600 ; $h = floor($h) ; $min = $h * 3600 ; $min = $amount - $min ; $min = $min / 60 ; $min = floor($min) ; $sec = ($min * 60) + ($h * 3600); $sec = $amount - $sec ; $return = "$h:$min:$sec" ; return $return ; } $second = 7291 ; $time = duration($second) ; echo $time ; Quote Link to comment https://forums.phpfreaks.com/topic/246686-changing-duration-in-seconds-to-hours-and-minutes/#findComment-1266790 Share on other sites More sharing options...
funstein Posted September 8, 2011 Share Posted September 8, 2011 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/246686-changing-duration-in-seconds-to-hours-and-minutes/#findComment-1266870 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.