PHP Time arithmetic

Source: Internet
Author: User

In many occasions in order to show the timeliness of information, the time is generally displayed as "just", "5 minutes Ago", "3 hours ago", and so on, rather than directly print out the time. such as microblogging, SNS applications for the longest use of this feature. The time format typically stored in the database is a UNIX timestamp, so here is a PHP function that converts the UNIX timestamp to the timeline display.
<?Phpdate_default_timezone_set (' PRC ');$date= "1351836000";EchoTrantime ($date);functionTransfer_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.‘ 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;}?>
Note that the parameter $time in the function transfer_time () must be a UNIX timestamp, and if not, convert it to a UNIX timestamp first with strtotime ().

PHP Time arithmetic

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.