Home > Backend Development > PHP Tutorial > QQ space timeline PHP implements timeline function code

QQ space timeline PHP implements timeline function code

WBOY
Release: 2016-07-29 08:47:00
Original
1204 people have browsed it

This article will introduce how to implement time conversion based on the timeline.
First we need to understand several functions of time:
time(): Returns the current Unix timestamp
date(): Format a local time/date.
Application example:

Copy code The code is as follows:


date("Y-m-d H:i:s",time()); //Format the current time, output: 2011-9-24 07: 27:35


strtotime(): Parse any English text datetime description into a Unix timestamp.
Application example:

Copy the code The code is as follows:


echo strtotime("+1 day"), "n"; //Output the timestamp 1 day ago: 1316932222


date_default_timezone_set(): Set the default time zone to use.
Generally we set Beijing time: date_default_timezone_set("PRC");
After understanding the above functions, we 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 The difference is compared with the time range (converted into seconds), and different results are output according to the range of the time axis (for example: 5 minutes ago). For ease of calculation, we convert the times into Unix timestamps.

Copy the code The 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 $time in function tranTime() must be a Unix timestamp. If not, please use strtotime() to convert it to a Unix timestamp first. The above code is easy to understand at a glance, so there is no need to elaborate further.
Call the function and output directly:

Copy the code The code is as follows:


$times="1316932222";
echo tranTime($times);

The above introduces the qq space time axis PHP implementation of the timeline function code, including the content of the qq space time axis. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template