Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Display timer in m:s than just seconds

Comments in 'Plugin Development' started by Kabluinc, Aug 31, 2016.

  1. Kabluinc
    Offline

    Kabluinc New Member

    Joined:
    Mar 2, 2016
    Posts:
    29
    Minecraft User:
    Kabluinc
    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
  2. Survingo
    Offline

    Survingo Active Member

    Joined:
    Dec 6, 2015
    Posts:
    221
  3. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    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($time1);//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 600);

    if(
    $sec >= -and $sec <= 1){
    //Do stuff, one minute passed
    --$min;
    $sec round($min 600);
    }else{
    --
    $sec;
    }

    if(
    $min >= -and $min <= 0
    and $sec >= -and $sec <= 1){
    //Finished timer
    return;
    }

    //Timer ongoing, send timer message







    Last edited: Aug 31, 2016
    XShockinFireX likes this.
  4. Irish
    Offline

    Irish Active Member

    Joined:
    Apr 24, 2016
    Posts:
    96
    Minecraft User:
    _Irish_
    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);
    }
  5. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    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.
  6. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    OMG How did I ever miss that. I used a way that is 2x slower for all my minigames...
  7. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    I don't think this is simple :p
  8. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    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.
    applqpak likes this.
  9. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    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?
  10. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    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.
  11. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    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($time1); // 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 600);

    if(
    $sec >= -and $sec <= 1){ // Do stuff, one minute passed
        
    --$min;
        
    $sec round($min 600);
    }else{
        --
    $sec;
    }

    if(
    $min >= -and $min <= and $sec >= -and $sec <= 1){
        
    // Finished timer
        
    return;
    }

    // Timer ongoing, send timer message
    MyNameIsTriXz likes this.
  12. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    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
  13. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    Perhaps, because I was just correcting the spaces?
    applqpak and Gamecrafter like this.
  14. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    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...
    Last edited: Sep 27, 2016
  15. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    *complex
    applqpak likes this.
  16. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Fixed
    applqpak and Vaivez66 like this.
  17. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    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. :p
    Vaivez66 likes this.
  18. Jazzwhistle
    Offline

    Jazzwhistle Notable Member

    Joined:
    Dec 27, 2014
    Posts:
    364
    Minecraft User:
    Awzaw
    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';
        }
    Vaivez66 likes this.
  19. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    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?
  20. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    This is a fine way to end a bad discussion.;)

    We can't judge but we can give opinions.
    Vaivez66 likes this.

Share This Page

Advertisement