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

QQ space timeline PHP implements timeline function code

Jul 29, 2016 am 08:47 AM

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.

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

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 cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

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

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles