Jump to content

Changing duration in seconds to hours and minutes


etrader

Recommended Posts

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

 

 

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

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

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 ;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.