PHP Display article date one hour before the day before the month before a year ago

Source: Internet
Author: User
Tags date timestamp format string format

<?

/**
* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.