1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

How to change second to min or hour

Discussion in 'Development' started by Hoyee, Jun 10, 2020.

  1. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    How can I do? like it was 90second left but changes to 1min 30sec left
     
  2. SalmonDE

    SalmonDE Zombie Pigman

    Messages:
    739
    GitHub:
    SalmonDE
    Given the number of seconds left, you can use division and rounding to convert to a whole amount in a larger unit. After that the modulo operator can determine the remaining seconds.

    Example:

    PHP:
    $totalSeconds 90;

    $minutes floor($totalSeconds 60); // the divisor is 60 because 60 seconds fit into 1 minute
    $seconds $totalSeconds 60;

    echo 
    $minutes.":".$seconds;
    // output: 1:30
    A similar approach for hours:

    PHP:
    $totalSeconds 3800;

    $minutes floor($totalSeconds 60);
    $seconds $totalSeconds 60;

    $hours floor($minutes 60); // the divisor is still 60 because 60 minutes fit into 1 hour
    $minutes $minutes 60;

    echo 
    $hours.":".$minutes.":".$seconds;
    // output: 1:3:20
     
    Last edited: Jun 10, 2020
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.