Quote:
Originally Posted by edon1337
how would you convert 3:15 to seconds? It's not even a number.
|
Grab the value before the : and multiply it by 60. Then add the value that was after the :.
If you wanted to go the opposite direction, take the total seconds value and divide it by 60. That's your minutes. Then take the total seconds value and modulo it by 60. There's the rest of the seconds.
Here's the equivalent code that you'd find in a Sourcemod plugin, with some tweaks to make it work if they included hours.
PHP Code:
stock int HMSDurationToSeconds(char[] duration) {
char timeunits[3][8]
int numStrings = ExplodeString(duration, ":", timeunits, sizeof(timeunits), sizeof(timeunits[]));
if (!numStrings) return 0;
int numSeconds;
numSeconds += StringToInt(timeunits[0]);
if (numStrings > 1) {
numSeconds *= 60;
numSeconds += StringToInt(timeunits[1]);
}
if (numStrings > 2) {
numSeconds *= 60;
numSeconds += StringToInt(timeunits[2]);
}
return numSeconds;
}
stock void SecondsToHMSDuration(int totalSeconds, char[] timeString) {
int hours = totalSeconds / (60*60);
int minutes = totalSeconds % (60 * 60) / 60;
int seconds = totalSeconds % 60;
if (hours) Format(timeString, 10, "%i:%02i:%02i", hours, minutes, seconds);
else Format(timeString, 10, "%i:%02i", minutes, seconds);
}
__________________