NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page [PHP] Converting seconds too minutes and seconds

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search
7 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 01-09-2007, 11:21 AM THREAD STARTER               #1 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,074
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009

[PHP] Converting seconds too minutes and seconds


I was on IRC earlier and someone posted a question on how they could convert x amount of seconds into minutes and seconds so I decided to write a little tutorial. For a seasoned php programmer this is very basic stuff but I thought it may come in handy for anyone new to php.

The following is the full function:-

PHP Code:
function sec_to_min($sec)
{
    
$seconds $sec 60;
    
$minutes = ($sec-$seconds)/60;
    
$min_sec $minutes.':'.str_pad($seconds2"0"STR_PAD_LEFT);
    return 
$min_sec;

Now lets break this up (it is as it looks) very simple.

Firstly we create the function

PHP Code:
function sec_to_min($sec
this tells the php engine that we are creating a function called sec_to_min and that it will have 1 input which in this case will be called $sec (this is how it will be used within the function).

Now the next line basically finds out how many seconds are there that will not be included as part of a minute. For example if you have 130 seconds we know that the remainder which will be seconds is 10. An easy way to find out this is to use the modulus arithmetic operator (%) so to work this out for the 70 second example we do the following:-

PHP Code:
130 60 10 
so to do this in php we do:-
PHP Code:
$seconds 130 60
as amount of seconds is dynamic and can be anything we can replace the amount of seconds with the variable that contains this value which in our case is $sec:-

PHP Code:
$seconds $sec 60
$seconds will now hold the amount of seconds that were left over.
????: NamePros.com http://www.namepros.com/code/279150-php-converting-seconds-too-minutes-seconds.html

Next we have to work out how many minutes are there which is a simple mathematical equation. Going back to our 130 second example it would be:-

PHP Code:
$minutes = (130-$seconds)/60
130 being the amount of seconds passed to the function, so we minus the remainder seconds from the total (which was held in the $seconds variable from a minute ago). Once this calculation is worked out we simply divide the total by 60 (the amount of seconds in a minute). We can be sure that this is a whole number as we have already taken the remainder away.

You may be wondering why we have brackets around part of the equation. The reason is that in php different arithmetic operators take precedence (as they also do in normal mathematics) so instead of working left to right the most important are calculated first, having the section in brackets ensures that this sum will work out correctly the way we planned.

after the calculation has been carried out $minutes holds how many whole minutes are in the seconds.

We can now format the minutes and seconds as needed. In this function I have decided to use : to separate the minutes and seconds.

PHP Code:
$min_sec $minutes.':'.str_pad($seconds2"0"STR_PAD_LEFT); 
as you see I have used a str_pad() function on the seconds variable. The reason I have done this is because say for example you have 61 seconds the remainder of seconds would be 1 but the convention when we write minutes and seconds is of course to have seconds as 2 characters so I have told str_pad that seconds must be 2 characters long, if it is not it should put 0's to the left of the number until it is.

We can now simply make $min_sec available to outside of the function:-

PHP Code:
return $min_sec
Some people do echo $min_sec but this cis not the proper way to do it, If you use echo it will output it straight to the browser. If we use return it will make it available as in the following example:-

PHP Code:
<?PHP
function sec_to_min($sec)
{
    
$seconds $sec 60;
    
$minutes = ($sec-$seconds)/60;
    
$min_sec $minutes.':'.str_pad($seconds2"0"STR_PAD_LEFT);
    return 
$min_sec;
}
$secs 1000;
$output sec_to_min($sec);
echo 
$output;
?>
If we want to in the above example we can do anything with $output before we echo it to the browser. If we used echo in the function instead of return, $output would not contain anything and we could not do anything with it.

Hopefully this little tutorial has been of use to a few people who are starting out in php. It is not exactly an in depth tutorial but maybe useful for any newcomers to php.
????: NamePros.com http://www.namepros.com/showthread.php?t=279150

Ways in which this could be enhanced:-

Ensure the input is actually of numerical value.
Make it more aware of wether any minutes exist.
Enhance it so that it can also work with hours, days etc.
Peter is offline  
Old 01-09-2007, 12:42 PM   #2 (permalink)
NamePros Regular
 
beaver6813's Avatar
Join Date: May 2005
Location: England
Posts: 390
beaver6813 is a jewel in the roughbeaver6813 is a jewel in the roughbeaver6813 is a jewel in the rough
 




Great lil script! Unless this is going to be feeding straight from user input then checking for numeric shouldn't be needed... although is_numeric() can always do that easy, keeping the function small is quite nice Rep+
beaver6813 is offline  
Old 01-09-2007, 01:49 PM   #3 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,792
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
Instead of checking that the variable is numeric, I just use (int) $variable to make it one.
Dan is offline  
Old 01-09-2007, 11:32 PM THREAD STARTER               #4 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,074
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
is_numeric is not actually the best way to check it (nor for that matter is using (int). You should use ctype_digit which check that there is actually a number in the string as well as it being 100% numbers. is_numeric passes even if the string is empty.

Oh and thank you for the rep beaver6813, much appreciated.
Peter is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 03:21 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger