PHP CodeBase: Show time as "just" "N Minutes/hour ago" method to explain _php tips

Source: Internet
Author: User
In many occasions in order to show the timeliness of information, the general will show the time "just", "5 minutes Ago", "3 hours ago," and so on, rather than directly to the time to print out. For example microblogging, SNS class application is the longest use this function. The time format typically stored in the database is the UNIX timestamp, so a PHP function that converts the UNIX timestamp to the timeline display is recorded here.
The function is relatively simple, look directly at the code is very understood.
Copy Code code as follows:

<?php
Date_default_timezone_set (' PRC ');
$date = "1351836000";
echo Trantime ($date);
function Transfer_time ($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 = ' Day before yesterday '. $rtime;
}
Else
{
$str = $rtime;
}
return $str;
}
?>

Note the parameter $time in the function transfer_time () must be a Unix timestamp, and if not, use Strtotime () to convert it to a UNIX timestamp

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.