QQ space timeline PHP implements timeline function code
Jul 29, 2016 am 08:47 AMThis 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.

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon
