TimeAgo::dateDifference PHP Method

dateDifference() public method

public dateDifference ( $past, $now = "now" )
    public function dateDifference($past, $now = "now")
    {
        // initializes the placeholders for the different "times"
        $seconds = 0;
        $minutes = 0;
        $hours = 0;
        $days = 0;
        $months = 0;
        $years = 0;
        // 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);
        // calculates the difference
        $timeDifference = $now - $past;
        // starts determining the time difference
        if ($timeDifference >= 0) {
            switch ($timeDifference) {
                // finds the number of years
                case $timeDifference >= $this->secondsPerYear:
                    // uses floor to remove decimals
                    $years = floor($timeDifference / $this->secondsPerYear);
                    // saves the amount of seconds left
                    $timeDifference = $timeDifference - $years * $this->secondsPerYear;
                    // finds the number of months
                // finds the number of months
                case $timeDifference >= $this->secondsPerMonth && $timeDifference <= $this->secondsPerYear - 1:
                    // uses floor to remove decimals
                    $months = floor($timeDifference / $this->secondsPerMonth);
                    // saves the amount of seconds left
                    $timeDifference = $timeDifference - $months * $this->secondsPerMonth;
                    // finds the number of days
                // finds the number of days
                case $timeDifference >= $this->secondsPerDay && $timeDifference <= $this->secondsPerYear - 1:
                    // uses floor to remove decimals
                    $days = floor($timeDifference / $this->secondsPerDay);
                    // saves the amount of seconds left
                    $timeDifference = $timeDifference - $days * $this->secondsPerDay;
                    // finds the number of hours
                // finds the number of hours
                case $timeDifference >= $this->secondsPerHour && $timeDifference <= $this->secondsPerDay - 1:
                    // uses floor to remove decimals
                    $hours = floor($timeDifference / $this->secondsPerHour);
                    // saves the amount of seconds left
                    $timeDifference = $timeDifference - $hours * $this->secondsPerHour;
                    // finds the number of minutes
                // finds the number of minutes
                case $timeDifference >= $this->secondsPerMinute && $timeDifference <= $this->secondsPerHour - 1:
                    // uses floor to remove decimals
                    $minutes = floor($timeDifference / $this->secondsPerMinute);
                    // saves the amount of seconds left
                    $timeDifference = $timeDifference - $minutes * $this->secondsPerMinute;
                    // finds the number of seconds
                // finds the number of seconds
                case $timeDifference <= $this->secondsPerMinute - 1:
                    // seconds is just what there is in the timeDifference variable
                    $seconds = $timeDifference;
            }
        }
        $difference = array("years" => $years, "months" => $months, "days" => $days, "hours" => $hours, "minutes" => $minutes, "seconds" => $seconds);
        return $difference;
    }

Usage Example

Exemplo n.º 1
0
echo "<br/><strong>rule 11</strong> (2 yrs <-> max time or date # => over [2..X] years)<br/>";
$timeAgo = new TimeAgo();
echo $timeAgo->inWords("2009/4/26 00:00:00", "2011/4/26 00:00:00");
echo "<br/>";
$timeAgo = new TimeAgo();
echo $timeAgo->inWords("2005/4/26 00:00:00", "2011/4/26 00:00:00");
echo "<br/>";
$timeAgo = new TimeAgo();
echo $timeAgo->inWords("1999/4/26 00:00:00", "2011/4/26 00:00:00");
echo "<br/>";
echo "</p>";
echo "<h2>TimeAgo class tests (dateDifference)</h2>";
echo "<p>";
$timeAgo = new TimeAgo();
echo "<pre>";
print_r($timeAgo->dateDifference("2010/4/01 00:00:00", "2010/5/12 03:05:30"));
echo "</pre>";
echo "</p>";
echo "<h1>WWDateTime class tests!</h1>";
function test_time($timeAgo, $timeAsItShouldBe)
{
    echo "<p>";
    $datetime = new WWDateTime($timeAgo);
    echo $datetime->format(DATE_RFC3339);
    echo " = ";
    echo $datetime->timeAgoInWords();
    echo " === ";
    echo $timeAsItShouldBe;
    echo "</p>";
}
test_time("-2 year", "over 2 years");