Ignore:
Timestamp:
Jun 23, 2006 10:00:21 PM (18 years ago)
Author:
scdev
Message:

Q - set limit to the length of logged messages. Added timeElapsed to Utilities.inc.php.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Utilities.inc.php

    r159 r180  
    270270
    271271    return sprintf($format, $size, $units[$i]);
     272}
     273
     274/*
     275* Returns a human readable amount of time elapsed since the given unix timestamp.
     276*
     277* 45 seconds
     278* 12 minutes
     279* 3.5 hours
     280* 2 days
     281* 1 week
     282* 4 months
     283*
     284* Months are calculated using the real number of days in a year: 365.2422 / 12.
     285*
     286* @access   public
     287* @param    int $time Unix timestamp of date to find elapsed from.
     288* @param    string $max_unit Key value from the $units array.
     289* @param    string $format Sprintf formatting string.
     290* @return   string Value of units elapsed.
     291* @author   Quinn Comendant <quinn@strangecode.com>
     292* @version  1.0
     293* @since    23 Jun 2006 12:15:19
     294*/
     295function timeElapsed($time, $max_unit=null, $format='%01.1f')
     296{
     297    if ($time <= time()) {
     298        $elapsed = $time_diff = time() - $time;
     299    } else {
     300        $elapsed = $time_diff = $time - time();
     301    }
     302   
     303    // Units
     304    $units = array(
     305        'second' => array(1, _("second"), _("seconds")),
     306        'minute' => array(60, _("minute"), _("minutes")),
     307        'hour' => array(3600, _("hour"), _("hours")),
     308        'day' => array(86400, _("day"), _("days")),
     309        'week' => array(604800, _("week"), _("weeks")),
     310        'month' => array(2629743.84, _("month"), _("months")),
     311        'year' => array(31556926.08, _("year"), _("years")),
     312        'decade' => array(315569260.8, _("decade"), _("decades")),
     313    );
     314   
     315    // Max unit
     316    $max_unit = isset($units[$max_unit]) ? $max_unit : 'decade';
     317
     318    foreach ($units as $k => $v) {
     319        if ($max_unit != $k && $time_diff >= $v[0]) {
     320            $elapsed = $time_diff / $v[0];
     321            $last_unit = $k;
     322        }
     323    }
     324    $elapsed = sprintf($format, $elapsed);
     325    return sprintf('%s %s', $elapsed, (1 == $elapsed ? $units[$last_unit][1] : $units[$last_unit][2]));   
    272326}
    273327
Note: See TracChangeset for help on using the changeset viewer.