Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2011
    Posts
    113
    Thanks
    9
    Thanked 1 Time in 1 Post

    HELP Display different times for hours.

    Code:
    DB
    User | Hours
    1        13
    2        982
    3        145
    4        200
    So say for User 2 is 982 which is 40.916 days I want to display it like this:
    Code:
    1 Month 10 days 9 hours
    And user 3
    145 - 6.0416 Days Display:
    Code:
    6 Days 42 minutes
    User 4
    200 - 8.33 days
    Code:
    1 Week 1 day 3 hours
    etc for years also

  • #2
    Regular Coder
    Join Date
    Oct 2011
    Posts
    113
    Thanks
    9
    Thanked 1 Time in 1 Post
    I know it seems complicated but can anyone try an attempt?

  • #3
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    The function below should be OK. Some of the results are different than yours so it might need fine tuning.

    PHP Code:
    <?php

    function elapsed_time($seconds$precision 2) {

    $a = array('decade' => 315576000'year' => 31557600'month' => 2629800'week' => 604800'day' => 86400'hour' => 3600'min' => 60'sec' => 1);
      
    $i 0;
        foreach(
    $a as $k => $v) {
          $
    $k floor($seconds/$v);
          if ($
    $k$i++;
          
    $seconds $i >= $precision $seconds - $$k $v;
          
    $s = $$k 's' '';
          $
    $k = $$k ? $$k.' '.$k.$s.' ' '';
          @
    $result .= $$k;
        }

    return 
    $result;
    }


    $hours=13;
    $seconds =($hours*3600);
    echo 
    elapsed_time($seconds6).'<br />';

    $hours=982;
    $seconds =($hours*3600);
    echo 
    elapsed_time($seconds6).'<br />';

    $hours=145;
    $seconds =($hours*3600);
    echo 
    elapsed_time($seconds6).'<br />';

    $hours=200;
    $seconds =($hours*3600);
    echo 
    elapsed_time($seconds6).'<br />';

    ?>

    Ouput
    Code:
    13 hours
    1 month 1 week 3 days 11 hours 30 mins
    6 days 1 hour
    1 week 1 day 8 hours
    Last edited by Len Whistler; 10-07-2012 at 05:30 AM.
    Leonard Whistler

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    They're different because your numbers are accurate (-ish, minus the month one which simply cannot be calculated properly). Since these are in hours with no indication of a fraction available, there is therefore no way to generate any time block < 1 hour in size (so no minutes and no seconds).

    There is of course no accurate way to perform this without knowing the starting date. Month is an ambiguous term with days qualifying as anywhere between 28 and 31 (72 hour variation). This is the same reason why things like DateInterval also cannot perform roll-overs as it could determine 32 days as anywhere between 1 month and 1 day or 1 month and 4 days.


  •  

    Posting Permissions

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