<?
/**
* Incoming date format or timestamp format time, returns the gap with the current time, such as 1 minutes ago, 2 hours ago, May ago, 3 years ago, etc.
* @param string or int $date divided into two date formats "2013-12-11 14:16:12" or timestamp format "1386743303"
* @param int $type
* @return String
*/
function Formattime ($date = 0, $type = 1) {//$type = 1 is timestamp format, $type = 2 is the date time format
Date_default_timezone_set (' PRC '); Set into China's time zone
Switch ($type) {
Case 1:
$date timestamp format
$second = Time ()-$date;
$minute = Floor ($second/60)? Floor ($second/60): 1; Get the number of minutes
if ($minute >= && $minute < (60 * 24)) {//minutes greater than or equal to 60 minutes and less than the number of minutes of the day, which is displayed by the hour
$hour = Floor ($minute/60); Get the number of hours
} elseif ($minute >=) && $minute < (60 * 24 * 30)) {//If the number of minutes is greater than the number of minutes in a day and the number of minutes is less than January, the display is displayed by day
$day = Floor ($minute/(60 * 24)); Get days
} elseif ($minute >=) && $minute < (60 * 24 * 365)) {//If the number of minutes is greater than or equal to January and is less than the number of minutes in a year, the monthly display
$month = Floor ($minute/(60 * 24 * 30)); Get the number of months
} elseif ($minute >= (60 * 24 * 365)) {//If the number of minutes is greater than the number of minutes per year, the year is displayed
$year = Floor ($minute/(60 * 24 * 365)); Get the number of years
}
Break
Case 2:
$date for string format 2013-06-06 19:16:12
$date = Strtotime ($date);
$second = Time ()-$date;
$minute = Floor ($second/60)? Floor ($second/60): 1; Get the number of minutes
if ($minute >= && $minute < (60 * 24)) {//minutes greater than or equal to 60 minutes and less than the number of minutes of the day, which is displayed by the hour
$hour = Floor ($minute/60); Get the number of hours
} elseif ($minute >=) && $minute < (60 * 24 * 30)) {//If the number of minutes is greater than the number of minutes in a day and the number of minutes is less than January, the display is displayed by day
$day = Floor ($minute/(60 * 24)); Get days
} elseif ($minute >=) && $minute < (60 * 24 * 365)) {//If the number of minutes is greater than or equal to January and is less than the number of minutes in a year, the monthly display
$month = Floor ($minute/(60 * 24 * 30)); Get the number of months
} elseif ($minute >= (60 * 24 * 365)) {//If the number of minutes is greater than the number of minutes per year, the year is displayed
$year = Floor ($minute/(60 * 24 * 365)); Get the number of years
}
Break
Default
Break
}
if (Isset ($year)) {
Return $year. Published ' years ago ';
} elseif (Isset ($month)) {
Return $month. Released ' month ago ';
} elseif (Isset ($day)) {
Return $day. Released ' days ago ';
} elseif (Isset ($hour)) {
Return $hour. Released ' hours ago ';
} elseif (Isset ($minute)) {
Return $minute. Released ' minutes ago ';
}
}
Test code:
$time 1 = time ()-60 * 60 * 23;
Echo Formattime ($time 1, 1); Results: 23 hours ago
echo "$time 2 = ' 2013-11-11 11:16:12 '; Results: Released 3 years ago
Echo Formattime ($time 2, 2);
?>
PHP Display article date one hour before the day before the month before a year ago