Make WordPress Core


Ignore:
Timestamp:
09/25/2012 05:26:19 AM (13 years ago)
Author:
nacin
Message:

Introduce constants to allow for easier expression of time periods in seconds. Adds MINUTE_IN_SECONDS, HOUR_IN_SECONDS, DAY_IN_SECONDS, WEEK_IN_SECONDS, YEAR_IN_SECONDS. props nbachiyski, SergeyBiryukov. fixes #20987.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/formatting.php

    r21993 r21996  
    18991899        $datetime->setTimezone( new DateTimeZone('UTC') );
    19001900        $offset = $datetime->getOffset();
    1901         $datetime->modify( '+' . $offset / 3600 . ' hours');
     1901        $datetime->modify( '+' . $offset / HOUR_IN_SECONDS . ' hours');
    19021902        $string_gmt = gmdate($format, $datetime->format('U'));
    19031903
     
    19051905    } else {
    19061906        $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
    1907         $string_gmt = gmdate($format, $string_time - get_option('gmt_offset') * 3600);
     1907        $string_gmt = gmdate($format, $string_time - get_option('gmt_offset') * HOUR_IN_SECONDS);
    19081908    }
    19091909    return $string_gmt;
     
    19251925    preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches);
    19261926    $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
    1927     $string_localtime = gmdate($format, $string_time + get_option('gmt_offset')*3600);
     1927    $string_localtime = gmdate($format, $string_time + get_option('gmt_offset') * HOUR_IN_SECONDS);
    19281928    return $string_localtime;
    19291929}
     
    19451945        $hours   = intval(substr($timezone, 1, 2));
    19461946        $minutes = intval(substr($timezone, 3, 4)) / 60;
    1947         $offset  = $sign * 3600 * ($hours + $minutes);
     1947        $offset  = $sign * HOUR_IN_SECONDS * ($hours + $minutes);
    19481948    }
    19491949    return $offset;
     
    19691969            $offset = iso8601_timezone_to_offset($date_bits[7]);
    19701970        } else { // we don't have a timezone, so we assume user local timezone (not server's!)
    1971             $offset = 3600 * get_option('gmt_offset');
     1971            $offset = HOUR_IN_SECONDS * get_option('gmt_offset');
    19721972        }
    19731973
     
    20942094 */
    20952095function human_time_diff( $from, $to = '' ) {
    2096     if ( empty($to) )
     2096    if ( empty( $to ) )
    20972097        $to = time();
    2098     $diff = (int) abs($to - $from);
    2099     if ($diff <= 3600) {
    2100         $mins = round($diff / 60);
    2101         if ($mins <= 1) {
     2098    $diff = (int) abs( $to - $from );
     2099    if ( $diff <= HOUR_IN_SECONDS ) {
     2100        $mins = round( $diff / MINUTE_IN_SECONDS );
     2101        if ( $mins <= 1 ) {
    21022102            $mins = 1;
    21032103        }
    21042104        /* translators: min=minute */
    2105         $since = sprintf(_n('%s min', '%s mins', $mins), $mins);
    2106     } else if (($diff <= 86400) && ($diff > 3600)) {
    2107         $hours = round($diff / 3600);
    2108         if ($hours <= 1) {
     2105        $since = sprintf( _n( '%s min', '%s mins', $mins ), $mins );
     2106    } elseif ( ( $diff <= DAY_IN_SECONDS ) && ( $diff > HOUR_IN_SECONDS ) ) {
     2107        $hours = round( $diff / HOUR_IN_SECONDS );
     2108        if ( $hours <= 1 ) {
    21092109            $hours = 1;
    21102110        }
    2111         $since = sprintf(_n('%s hour', '%s hours', $hours), $hours);
    2112     } elseif ($diff >= 86400) {
    2113         $days = round($diff / 86400);
    2114         if ($days <= 1) {
     2111        $since = sprintf( _n( '%s hour', '%s hours', $hours ), $hours );
     2112    } elseif ( $diff >= DAY_IN_SECONDS ) {
     2113        $days = round( $diff / DAY_IN_SECONDS );
     2114        if ( $days <= 1 ) {
    21152115            $days = 1;
    21162116        }
    2117         $since = sprintf(_n('%s day', '%s days', $days), $days);
     2117        $since = sprintf( _n( '%s day', '%s days', $days ), $days );
    21182118    }
    21192119    return $since;
Note: See TracChangeset for help on using the changeset viewer.