TimeAgo::inWords PHP Method

inWords() public method

public inWords ( $past, $time_language, $now = "now" )
    public function inWords($past, $time_language, $now = "now")
    {
        // sets the default timezone
        date_default_timezone_set($this->timezone);
        // finds the past in datetime
        $past = strtotime($past);
        // finds the current datetime
        $now = strtotime($now);
        // creates the "time ago" string. This always starts with an "about..."
        $timeAgo = "";
        // finds the time difference
        $timeDifference = $now - $past;
        // less than 29secs
        if ($timeDifference <= 29) {
            $timeAgo = $time_language['less_than_a_minute'];
        } else {
            if ($timeDifference > 29 && $timeDifference <= 89) {
                $timeAgo = $time_language['1_minute'];
            } else {
                if ($timeDifference > 89 && $timeDifference <= $this->secondsPerMinute * 44 + 29) {
                    $minutes = floor($timeDifference / $this->secondsPerMinute);
                    $timeAgo = str_replace('{x}', $minutes, $time_language['_minutes']);
                } else {
                    if ($timeDifference > $this->secondsPerMinute * 44 + 29 && $timeDifference < $this->secondsPerMinute * 89 + 29) {
                        $timeAgo = $time_language['about_1_hour'];
                    } else {
                        if ($timeDifference > $this->secondsPerMinute * 89 + 29 && $timeDifference <= $this->secondsPerHour * 23 + $this->secondsPerMinute * 59 + 29) {
                            $hours = floor($timeDifference / $this->secondsPerHour);
                            if ($hours == 1) {
                                $timeAgo = $time_language['about_1_hour'];
                            } else {
                                $timeAgo = str_replace('{x}', $hours, $time_language['_hours']);
                            }
                        } else {
                            if ($timeDifference > $this->secondsPerHour * 23 + $this->secondsPerMinute * 59 + 29 && $timeDifference <= $this->secondsPerHour * 47 + $this->secondsPerMinute * 59 + 29) {
                                $timeAgo = $time_language['1_day'];
                            } else {
                                if ($timeDifference > $this->secondsPerHour * 47 + $this->secondsPerMinute * 59 + 29 && $timeDifference <= $this->secondsPerDay * 29 + $this->secondsPerHour * 23 + $this->secondsPerMinute * 59 + 29) {
                                    $days = floor($timeDifference / $this->secondsPerDay);
                                    $timeAgo = str_replace('{x}', $days, $time_language['_days']);
                                } else {
                                    if ($timeDifference > $this->secondsPerDay * 29 + $this->secondsPerHour * 23 + $this->secondsPerMinute * 59 + 29 && $timeDifference <= $this->secondsPerDay * 59 + $this->secondsPerHour * 23 + $this->secondsPerMinute * 59 + 29) {
                                        $timeAgo = $time_language['about_1_month'];
                                    } else {
                                        if ($timeDifference > $this->secondsPerDay * 59 + $this->secondsPerHour * 23 + $this->secondsPerMinute * 59 + 29 && $timeDifference < $this->secondsPerYear) {
                                            $months = round($timeDifference / $this->secondsPerMonth);
                                            // if months is 1, then set it to 2, because we are "past" 1 month
                                            if ($months == 1) {
                                                $months = 2;
                                            }
                                            //$timeAgo = $months . $time_language['_months'];
                                            $timeAgo = str_replace('{x}', $months, $time_language['_months']);
                                        } else {
                                            if ($timeDifference >= $this->secondsPerYear && $timeDifference < $this->secondsPerYear * 2) {
                                                $timeAgo = $time_language['about_1_year'];
                                            } else {
                                                $years = floor($timeDifference / $this->secondsPerYear);
                                                $timeAgo = str_replace('{x}', $years, $time_language['over_x_years']);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return $timeAgo;
    }

Usage Example

Exemplo n.º 1
0
/**
 * Theme related functions. 
 *
 */
function getTimeAgo($timestamp)
{
    $date = date("Y-m-d H:i:s", $timestamp);
    $timeAgo = new TimeAgo();
    $time = $timeAgo->inWords($date);
    return $time;
}
All Usage Examples Of TimeAgo::inWords