Jump to content

Changing duration in seconds to hours and minutes


etrader
 Share

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ;

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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.