PHP implementation of timeline function code_PHP tutorial

WBOY
Release: 2016-07-21 15:24:05
Original
1334 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 the date and time description of any English text into a Unix timestamp.
Application example:
Copy 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 be used.
Generally we set Beijing time: date_default_timezone_set("PRC");
After understanding the above functions, we can write the timeline function:
The principle of this function is to compare the current system time with the target time, and get A difference value, and then compare the difference value with the time range (converted into seconds), and output different results 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 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.'hours 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 Unix time Stamp, 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);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324367.htmlTechArticleThis 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(): formats a local time/...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!