| | |||||
| ||||||||
| 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. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| NamePros Expert Join Date: Nov 2003 Location: Scotland
Posts: 5,074
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | [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: Firstly we create the function PHP Code: 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: PHP Code: PHP Code: ????: 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: 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: We can now simply make $min_sec available to outside of the function:- PHP Code: PHP Code: 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. |
| |
| | #2 (permalink) |
| NamePros Regular Join Date: May 2005 Location: England
Posts: 390
![]() ![]() ![]() | 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.com - Web Developer Extraordinaire! |
| |
| | THREAD STARTER #4 (permalink) |
| NamePros Expert Join Date: Nov 2003 Location: Scotland
Posts: 5,074
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | 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. |
| |