Hi. In my minigames plugin the timer only displays as seconds e.g. 560 or 450. How can i convert this to minutes and seconds? Like 02:25 for 2 mins 25 seconds. The way it gets time is $config->get("playTime); where playtime is in seconds format But i want that when it displays timer in popup it displays in m:s
http://stackoverflow.com/questions/3534533/output-is-in-seconds-convert-to-hhmmss-format-in-php Like that?
Simple: PHP: $time = (int) $config->get("playTime");While(!is_int($time)){$time = preg_replace("/[^0-9]/", "", $time);if(is_numeric($time){$seconds = (int) $time;}if(!is_int($seconds)) return;}$min = round($time, 1);//will display as : {3.0, 2.9, 2.8, 2.7, 2.6, 2.5}, etc. If you wanna display as : {3, 2, 1} set 1(one) to 0 (zero)$sec = round($time % 60, 0);if($sec >= -1 and $sec <= 1){//Do stuff, one minute passed--$min;$sec = round($min % 60, 0);}else{--$sec;}if($min >= -1 and $min <= 0and $sec >= -1 and $sec <= 1){//Finished timerreturn;}//Timer ongoing, send timer message
this is god awful code use this: PHP: public function s2s($i){$s = floor($i%60);$m = floor($i/60);return (($m < 10 ? "0" : "") . $m . ":" . ($s < 10 ? "0" : "") . $s);}
Why such a big code when there already is a function for it? Code: PHP: gmdate("i:s", $seconds); I use it for my minigame and it works well.
Simple for those who know PHP, and not just a shitty code like those that are too lazy to make a good plugin. The code is for the whole task not just converting it to y/d/h/m/s.
Doesn't mean that every good plugin has a complicated code like that. On your code, what's the point by checking with is_int(), if you've already casted it with (int)? And, I'm sure that there are more effective and simple ways that you know. Moreover, he just needs a way to convert the second format to minute. Your code just makes him confused, and proves that you're trying to show how professional you are. Lastly, don't forget to use add spaces in code. That makes it look readable, doesn't it?
It's simple, not "complicated". If you truly want to see complex code, find some C++ examples. Oh, yeah, right; no "point" on checking if is int; except if the config has special characters in the string. e.g. "123-456-789" You are correct, he would be confused if I didn't put comments in the code. I'm not trying to be professional, just want things right. Add spaces where? Specify.
This shall do PHP: $time = (int) $config->get("playTime");while(!is_int($time)){ $time = preg_replace("/[^0-9]/", "", $time); if(is_numeric($time){ $seconds = (int) $time; } if(!is_int($seconds)) return;}$min = round($time, 1); // will display as : {3.0, 2.9, 2.8, 2.7, 2.6, 2.5}, etc. If you wanna display as : {3, 2, 1} set 1(one) to 0 (zero)$sec = round($time % 60, 0);if($sec >= -1 and $sec <= 1){ // Do stuff, one minute passed --$min; $sec = round($min % 60, 0);}else{ --$sec;}if($min >= -1 and $min <= 0 and $sec >= -1 and $sec <= 1){ // Finished timer return;}// Timer ongoing, send timer message
Checking if $time is an integer is useless, you've already casted it to an integer. And you're supposed to be clever, not complex. More code isn't always better. Less code isn't always better also. It's efficiency and readability that counts. Why use that when there's already a built-in function for it? Why are you still redundantly checking if the variables are integers?!? More code doesn't mean better. As I said above, efficiency and readability is most important. Simpler solutions are better. Perhaps this Quora post will clear up how you view this, and give you an idea how I'm viewing this: https://www.quora.com/How-do-I-trai...h-fewer-bugs/answer/Glyn-Williams?ref=fb_page
True! Complex programming might make you feel proud because you made something difficult, but on the other side you can't share it that easy with other people and edit that code, because you need some time to remind what these functions mean...
You people think I wrote this to show off complex code? No, it's my style. You guys don't get my point, I said it, you need to try to understand it, not just read and get mad.
I use this, but you'll have to replace the separators with colons if you prefer: PHP: function timeSpent($start, $s) { $t = array( 'd' => 86400, 'h' => 3600, 'm' => 60 ); $s = abs($s - $start); $stringtemp = ""; foreach ($t as $key => &$val) { $$key = floor($s / $val); $s -= ($$key * $val); $stringtemp .= ($$key == 0) ? '' : $$key . "$key "; } return $stringtemp . $s . 's'; }
It's okay, man. I'm sorry if I was being rude to you, but really, I didn't mean to do that. Let's try not to make these arguments go any further, alright? We all know that everyone has their own coding style, therefore, we aren't supposed to blame each other for that. Can we?