Phpcms V9 According to just, 1 minutes ago, 1 hours ago, a few days ago to display the article release date

Source: Internet
Author: User

Save the following instance code as time.php
Delete

The code is as follows Copy Code
Test code:
$time 1 = time ()-60 * 60 * 23;
Echo Formattime ($time 1, 1); Result: 23 hours ago
echo "<hr/>";
$time 2 = ' 2013-11-11 19:16:12 '; Result: 29 days ago
Echo Formattime ($time 2, 2);

index.php in the root directory of the program
Join
Include Phpcms_path. ' /time.php ';
Join at the point where time is displayed
Time Call code {formattime ($r [inputtime],1)}

The code is as follows Copy Code


<?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 "<hr/>";
$time 2 = ' 2013-11-11 19:16:12 '; Result: 29 days ago
Echo Formattime ($time 2, 2);
?>

Related Article

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.

Tags Index: