PHP implements the Timeline function (personalization time)

Source: Internet
Author: User
When we post, comment on the forum, or use QQ space to publish the blog, Weibo, we will see the published content after the time displayed as "just", "5 minutes Ago", "Yesterday 10:23", and not directly display the specific date and time.
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:
Date ("Y-m-d h:i:s", Time ());
Format current time, output: 2010-10-11 05:27:35
Strtotime (): Resolves the datetime description of any English text to a Unix timestamp.
Application Examples:
Echo strtotime ("+1 Day"), "\ n";
Output 1 days ago Timestamp: 1286861475
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.
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:
$times = "1286861696";
echo Trantime ($times);
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.