This article describes how to implement a time-based transformation of the timeline.
First we need to understand several functions of time:
Time (): Returns the current Unix timestamp
Date (): Formats a local time/date.
Application Examples:
Copy CodeThe code is as follows:
Date ("Y-m-d h:i:s", Time ()); Format current time, output: 2011-9-24 07:27:35
Strtotime (): Resolves the datetime description of any English text to a Unix timestamp.
Application Examples:
Copy CodeThe code is as follows:
Echo strtotime ("+1 Day"), "\ n"; Output 1 days ago Timestamp: 1316932222
Date_default_timezone_set (): Sets the default time zone to use.
Generally we set up Beijing time: Date_default_timezone_set ("PRC");
After understanding the above functions we will write the timeline function:
The principle of this function is to compare the current time of the system with the target time, get a difference, and then compare the difference with the time range (converted to seconds), according to its range in the time axis output different results (such as: 5 minutes ago). For ease of calculation, we convert time to UNIX timestamps.
Copy CodeThe code is as follows:
function Trantime ($time) {
$rtime = Date ("m-d h:i", $time);
$htime = Date ("H:i", $time);
$time = Time ()-$time;
if ($time < 60) {
$str = ' just ';
}
ElseIf ($time < 60 * 60) {
$min = Floor ($time/60);
$str = $min. ' Minutes ago ';
}
ElseIf ($time < 60 * 60 * 24) {
$h = Floor ($time/(60*60));
$str = $h. ' Hour ago '. $htime;
}
ElseIf ($time < 60 * 60 * 24 * 3) {
$d = Floor ($time/(60*60*24));
if ($d ==1)
$str = ' Yesterday '. $rtime;
Else
$str = ' The day before yesterday '. $rtime;
}
else {
$str = $rtime;
}
return $str;
}
The parameter in function Trantime () must be a Unix timestamp, and if not, convert it to a UNIX timestamp by using Strtotime () first. The code above is understood at a glance, and no more should be said.
Call function, Direct output:
Copy CodeThe code is as follows:
$times = "1316932222";
echo Trantime ($times);
http://www.bkjia.com/PHPjc/324367.html www.bkjia.com true http://www.bkjia.com/PHPjc/324367.html techarticle This article describes how to implement a time-based transformation of the timeline. First we need to understand a few functions of time: Times (): Returns the current Unix timestamp date (): Formatting a local time/...