<?php /** * Incoming date format or timestamp format time, return to the current time gap, such as 1 minutes ago, 2 hours ago, May ago, 3 years ago * @param string or int $date two date formats "2013-12-11 14:16:12" or the timestamp format "1386743303" * @param int $type * @return String */ function Formattime ($date = 0, $type = 1) {//$type = 1 is timestamp format, $type = 2 is Date time format Date_default_timezone_set (' PRC '); Set to China's time zone Switch ($type) { Case 1: $date Time stamp 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 a day's minutes, that is, display by 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 less than the number of minutes in January, show by day $day = Floor ($minute/(60 * 24)); Get the number of days ElseIf ($minute >=) && $minute < (60 * 24 * 365)) {//If the number of minutes is greater than the number of minutes in January and less than one year, show monthly $month = Floor ($minute/(60 * 24 * 30)); Get the number of months } elseif ($minute >= (60 * 24 * 365)) {//If the number of minutes is greater than or equal to the number of minutes in a year, display by year $year = Floor ($minute/(60 * 24 * 365)); Get the number of years } Break Case 2: $date As 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 a day's minutes, that is, display by 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 less than the number of minutes in January, show by day $day = Floor ($minute/(60 * 24)); Get the number of days ElseIf ($minute >=) && $minute < (60 * 24 * 365)) {//If the number of minutes is greater than the number of minutes in January and less than one year, show monthly $month = Floor ($minute/(60 * 24 * 30)); Get the number of months } elseif ($minute >= (60 * 24 * 365)) {//If the number of minutes is greater than or equal to the number of minutes in a year, display by year $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. ' month ago '; } elseif (Isset ($day)) { Return $day. ' Days before the release '; } elseif (Isset ($hour)) { Return $hour. ' Hour before release '; } elseif (Isset ($minute)) { Return $minute. ' Minutes before the release '; } } Test code: $time 1 = time ()-60 * 60 * 23; Echo Formattime ($time 1, 1); Result: 23 hours ago echo "$time 2 = ' 2013-11-11 19:16:12 '; Result: 29 days ago Echo Formattime ($time 2, 2); ?> |