SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    function - convert time (not timestamp) into days, hours etc format

    I spent a while looking for this function and while I found one at php.net it required maths functions in php.

    What does it do?

    If you have a time difference, from basically a mktime() - another mktime() and you want to display this time (which is now just a number of seconds) you have a problem. None of the PHP functions will display it properly. Hence this function:
    PHP Code:

    function display_time($time) {
    // calculate components
    $days =    floor(($time / (60 60 24)));
    $time $time - ($days 60 60 24);
    $hours floor(($time / (60 60)));
    $time $time - ($hours 60 60);
    $minutes floor(($time 60));
    $time = ($time - ($minutes 60));
    $seconds $time;

    // format time
    $result $days ":";
    if (
    $hours 10) { 
         
    $result .= "0"

    $result .= $hours ":"
    if (
    $minutes 10) { 
         
    $result .= "0"

    $result .= $minutes ":"
    if (
    $seconds 10) { 
         
    $result .= "0"

    $result .= $seconds;

    return 
    $result;


    it will display the result like

    days:hours:minutes:seconds

    and will even add 0 in so for example you get

    23:01:42:05

    sort of thing.

    hope this is useful to someone. Any problems with it let me know.

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm, I'm a bit confused: what format would the argument ($time) passed to the function come in? Just a plain old number of seconds, convert into a number of hours, minutes, and seconds?

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yup.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •