SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: mktime()

  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mktime()

    Is there an easy way to make hours/mins/seconds out of a value of total seconds to using mktime() or is there an easy way to break it down?

    for example:

    PHP Code:
    $time_elapsed 86399;

    echo 
    mktime("H hours i minutes s seconds"$time_elapsed); 
    would print as '23 hours 59 minutes 59 seconds'

    thanks

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'll have to do it yourself (or look for it in a comment on the PHP manual site, because someone probably posted one).

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    Hailsham, UK
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A not so elegant way

    Code PHP:
    $time_elapsed = 86399;
    $hours = floor($time_elapsed/3600);
    $mins = floor(($time_elapsed-$hours*3600) / 60);
    $secs = $time_elapsed-($hours*3600 + $mins*60);
    echo $hours . ' hours ' . $mins . ' minutes ' . $secs . ' seconds';

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by calkie_f View Post
    A not so elegant way

    Code PHP:
    $time_elapsed = 86399;
    $hours = floor($time_elapsed/3600);
    $mins = floor(($time_elapsed-$hours*3600) / 60);
    $secs = $time_elapsed-($hours*3600 + $mins*60);
    echo $hours . ' hours ' . $mins . ' minutes ' . $secs . ' seconds';
    Thank you.

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A more elegant version, if you care:
    PHP Code:
    echo sprintf("%d hours, %d minutes and %d seconds"$time_elapsed 3600$time_elapsed 3600 60$time_elapsed 60); 
    Note: Doesn't work well for really high numbers.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


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
  •