1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to CONVERT seconds to minutes?

Discussion in 'PHP' started by BuildHome, Oct 27, 2007.

  1. #1
    Hello,

    I'm an ASP programmer but I have a PHP script but I don't know PHP :eek:

    I have a var that have int value of seconds, such as:
    $seconds = "150";
    PHP:
    How can I change it to minutes format, like:

    150 seconds => 02:30 minutes

    I will thank everyone who will be able to help me :rolleyes:

    Thanks.
     
    BuildHome, Oct 27, 2007 IP
  2. dpfreaks

    dpfreaks Peon

    Messages:
    37
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    echo floor($seconds/60) . ":" . $seconds % 60;
     
    dpfreaks, Oct 27, 2007 IP
    BuildHome likes this.
  3. legend2

    legend2 Well-Known Member

    Messages:
    1,537
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    115
    #3
    
    <?
    $seconds = "150";
    $minutes = floor($seconds/60);
    $secondsleft = $seconds%60;
    if($minutes<10)
    	$minutes = "0" . $minutes;
    if($secondsleft<10)
    	$secondsleft = "0" . $secondsleft;
    echo "$minutes:$secondsleft minutes";
    ?>
    
    PHP:
     
    legend2, Oct 27, 2007 IP
    BuildHome likes this.
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    
    <?php
    function minutes( $seconds )
    {
    	return sprintf( "%02.2d:%02.2d", floor( $seconds / 60 ), $seconds % 60 );
    }
    echo minutes( 150 );
    ?>
    
    PHP:
     
    krakjoe, Oct 27, 2007 IP
    BuildHome likes this.
  5. andrews

    andrews Peon

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    probably there's a better way...anyway I coded this and works

    <?PHP 
    
    function secondMinute($seconds){
    
        /// get minutes
        $minResult = floor($seconds/60);
        
        /// if minutes is between 0-9, add a "0" --> 00-09
        if($minResult < 10){$minResult = 0 . $minResult;}
        
        /// get sec
        $secResult = ($seconds/60 - $minResult)*60;
        
        /// if secondes is between 0-9, add a "0" --> 00-09
        if($secResult < 10){$secResult = 0 . $secResult;}
        
        /// return result
        echo $minResult,":",$secResult;
    
    }
    
    /// print
    secondMinute(150);
    ?>
    PHP:
    EDIT: there was no replies when I started to code it eheh, but now there are better ones :)
     
    andrews, Oct 27, 2007 IP
    BuildHome likes this.
  6. BuildHome

    BuildHome Well-Known Member

    Messages:
    837
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    108
    #6
    dpfreaks, thanks but I also wanted "0" before the time if it's lower than 10 minutes.

    legend2, that's what I wanted.
    You just forgot to add "s" to the "second" var in your second line, I've fixed it and now it's working GREAT!!

    krakjoe and andrews, Your code is also what I wanted. Very easy to understand & usage. Thanks!

    Thank you all my friends for your quick help!
    I've added to each of you a Rep :D
     
    BuildHome, Oct 27, 2007 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #7
    If there is a function for something, like formatting strings for instance, you should use it, there is less margin for error when you utilize functions that are tried and tested ... plus it's already been established by programmers far more experienced than yourself ( the php authors ) that this is the optimal way of carrying out a task ...
     
    krakjoe, Oct 27, 2007 IP
  8. BuildHome

    BuildHome Well-Known Member

    Messages:
    837
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    108
    #8
    Yes, I'm already using the function that you've wrote :)
     
    BuildHome, Oct 27, 2007 IP
  9. joberlin

    joberlin Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    ...quick and dirty 1 line version:

    $seconds = '150';
    echo (($min=floor($seconds/60))<10?'0':'').$min.":".(($sec=$seconds`)<10?'0':'').$sec;​
     
    joberlin, Dec 31, 2011 IP
  10. mukeshdak

    mukeshdak Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    It can not be more simpler then this one.
    echo gmdate("H:i:s", $time_in_seconds);
    PHP:
     
    mukeshdak, Jan 26, 2012 IP
  11. SingaHost

    SingaHost Guest

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    short and sweet!
     
    SingaHost, Feb 6, 2012 IP
  12. qwerty12

    qwerty12 Active Member

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    51
    #12
    i have already use it and works!! :D but i've to change " to ' and ' to " heheh
     
    qwerty12, Feb 6, 2012 IP
  13. ankit_88

    ankit_88 Greenhorn

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #13
    i am getting time in fraction like this 21.2...by using your code how can i get time in minute and second format.
     
    ankit_88, May 2, 2013 IP