- Impact
- 209
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:-
Now lets break this up (it is as it looks) very simple.
Firstly we create the function
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:-
so to do this in php we do:-
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:-
$seconds will now hold the amount of seconds that were left over.
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:-
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.
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:-
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:-
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.
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.
The following is the full function:-
PHP:
function sec_to_min($sec)
{
$seconds = $sec % 60;
$minutes = ($sec-$seconds)/60;
$min_sec = $minutes.':'.str_pad($seconds, 2, "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:
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:
130 % 60 = 10
so to do this in php we do:-
PHP:
$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:
$seconds = $sec % 60;
$seconds will now hold the amount of seconds that were left over.
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:
$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:
$min_sec = $minutes.':'.str_pad($seconds, 2, "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:
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:
<?PHP
function sec_to_min($sec)
{
$seconds = $sec % 60;
$minutes = ($sec-$seconds)/60;
$min_sec = $minutes.':'.str_pad($seconds, 2, "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.
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.






