WPDKDateTime::elapsedString PHP Method

elapsedString() public static method

For example if WordPress human_time_diff() display '10 hours', this method display '9 Hours 47 Minutes 56 Seconds'.
public static elapsedString ( integer $timestamp, boolean $hide_empty = true, integer $to, string $separator = ', ' ) : string
$timestamp integer Date from elapsed
$hide_empty boolean Optional. If TRUE '0 Year' will not return. Default TRUE.
$to integer Optional. Date to elapsed. If empty time() is used
$separator string Optional. Separator, default ', '.
return string
    public static function elapsedString($timestamp, $hide_empty = true, $to = 0, $separator = ', ')
    {
        // If no $to then now
        if (empty($to)) {
            $to = time();
        }
        // Key and string output
        $useful = array('y' => array(__('Year'), __('Years')), 'm' => array(__('Month'), __('Months')), 'd' => array(__('Day'), __('Days')), 'h' => array(__('Hour'), __('Hours')), 'i' => array(__('Minute'), __('Minutes')), 's' => array(__('Second'), __('Seconds')));
        $matrix = array('y' => array(12 * 30 * 24 * 60 * 60, 12), 'm' => array(30 * 24 * 60 * 60, 30), 'd' => array(24 * 60 * 60, 24), 'h' => array(60 * 60, 60), 'i' => array(60, 60), 's' => array(1, 60));
        $diff = $timestamp - $to;
        $stack = array();
        foreach ($useful as $w => $strings) {
            $value = floor($diff / $matrix[$w][0]) % $matrix[$w][1];
            if (empty($value) || $value < 0) {
                if ($hide_empty) {
                    continue;
                }
                $value = 0;
            }
            $stack[] = sprintf('%s %s', $value, _n($strings[0], $strings[1], $value));
        }
        return implode($separator, $stack);
    }