Converting seconds to 00:00:00 format

Discussion in 'PHP' started by Souljacker, Jun 1, 2008.

  1. #1
    I'm looking for a function that takes a numeric value in seconds and return it in a 00:00:00 format.

    For example, it takes 90 and returns 00:01:30

    Can anyone helpme here?
     
    Souljacker, Jun 1, 2008 IP
  2. GLD

    GLD Well-Known Member

    Messages:
    307
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    140
    #2
    Here you go:
    
    <?php
    
    function timeformat($sec){
    
    if($sec > 59){
    $min = $sec/60;
    $min = floor($min);
    $sec = $sec%60;
    }
    
    if($min > 59){
    $hour = $min/60;
    $hour = floor($hour);
    $min = $min - $hour * 60;
    } else {
    $hour = "00";
    }
    
    if(strlen($sec) == "1"){
    $sec = "0".$sec;
    }
    if(strlen($min) == "1"){
    $min = "0".$min;
    }
    if(strlen($hour) == "1"){
    $hour = "0".$hour;
    }
    return $hour . ":" . $min . ":" . $sec;
    
    }
    
    echo timeformat(90);
    
    ?>
    PHP:
    There's probably an easier way to do it, but that works fine.
     
    GLD, Jun 1, 2008 IP
  3. Souljacker

    Souljacker Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That's perfect. Thanks a lot
     
    Souljacker, Jun 1, 2008 IP