• I was just discovering that wordpress actually has a ‘time ago’ (human time difference) function. But I wondered if it could be edited in some way.

    Because once your posts (and comments) become years old, rather than days, then its more confusing to view “1268 days ago” instead of simply the date.

    I wonder if anyone can help me (or tell me how) if its possible to manage to get it to show “weeks ago”, “months ago” or even “years ago”.

    PS. Im somewhat of a newbie in PHP coding.

    /**
     * Determines the difference between two timestamps.
     *
     * The difference is returned in a human readable format such as "1 hour",
     * "5 mins", "2 days".
     *
     * @since 1.5.0
     *
     * @param int $from Unix timestamp from which the difference begins.
     * @param int $to Optional. Unix timestamp to end the time difference. Default becomes time() if not set.
     * @return string Human readable time difference.
     */
    function human_time_diff( $from, $to = '' ) {
    	if ( empty($to) )
    		$to = time();
    	$diff = (int) abs($to - $from);
    	if ($diff <= 3600) {
    		$mins = round($diff / 60);
    		if ($mins <= 1) {
    			$mins = 1;
    		}
    		/* translators: min=minute */
    		$since = sprintf(_n('%s min', '%s mins', $mins), $mins);
    	} else if (($diff <= 86400) && ($diff > 3600)) {
    		$hours = round($diff / 3600);
    		if ($hours <= 1) {
    			$hours = 1;
    		}
    		$since = sprintf(_n('%s hour', '%s hours', $hours), $hours);
    	} elseif ($diff >= 86400) {
    		$days = round($diff / 86400);
    		if ($days <= 1) {
    			$days = 1;
    		}
    		$since = sprintf(_n('%s day', '%s days', $days), $days);
    	}
    	return $since;
    }
  • The topic ‘Human time Difference edit?’ is closed to new replies.