PHP formatted timestamp displays friendly time, PHP formatted _php tutorial

Source: Internet
Author: User

PHP format timestamp displays friendly time, PHP formatted


All the time in the project was shown as 2014-10-20 10:22 and it looked stiff. In the microblogging, QQ space and other sites are usually displayed for a few seconds ago, a few minutes ago, a few hours ago, such as easy to read time, we call it a friendly time format. So how do you do it with PHP?

The general idea is as follows:

If it is cross-year and is greater than 3 days, it is displayed for a specific time

If it's Today's

If it's a minute, it's a few seconds ago.

If it's within an hour, it shows a few minutes ago.

If it is the same day and is greater than one hour, it will be displayed a few hours ago

If it was yesterday, what time did it show yesterday?

If it was the day before yesterday, what time did you see

If it is greater than three days (no cross-year), the number of months is displayed

According to the above ideas, it is not difficult to write the implementation code:

The implementation code is as follows:

//format friendly display timefunctionFormattime ($time){    $now= Time(); $day=Date(' y-m-d ',$time); $today=Date(' y-m-d '); $DAYARR=Explode('-',$day); $TODAYARR=Explode('-',$today); //distance of days, this method more than 30 days is not necessarily accurate, but 30 days is accurate, because one months may be 30 days may also be 31 days    $days=($TODAYARR[0]-$DAYARR[0]) *365+ (($TODAYARR[1]-$DAYARR[1]) *30) + ($TODAYARR[2]-$DAYARR[2]); //number of seconds to distance    $secs=$now-$time; if($TODAYARR[0]-$DAYARR[0]>0 &&$days&GT;3) {//spanning year and more than 3 days        return Date(' y-m-d ',$time); }Else{            if($days<1) {//Today            if($secs&LT;60)return $secs.' Seconds ago '; ElseIf($secs<3600)return  Floor($secs/60). " Minutes Ago "; Else return  Floor($secs/3600). " Hours ago "; }Else if($days&LT;2) {//Yesterday            $hour=Date(' H ',$time); return"Yesterday".$hour.' Point; }ElseIf($days&LT;3) {//The day before            $hour=Date(' H ',$time); return"The day before yesterday."$hour.' Point; }Else{//three days ago            return Date(' m-month d ',$time); }    }}

For reference only, please criticize or provide a better method. Blog Address: http://www.cnblogs.com/dragondean/


The PHP date function can format the timestamp as 2012-5-3

Date (' Y-n-j ', Time ()); The time () function generates a timestamp of the current time, which can be replaced by your own timestamp

Reference: cn2.php.net/manual/zh/function.date.php

PHP time Formatting

Simplest intercept field substr ("2010-11-23 02:10:45", 10)

-------------------
Oh, because when you save the time is not the timestamp, and the memory is the format of time, so there is no need to reformat it; If you really want to format again, then use Strtotime () to turn the timestamp, then date ("h_i_s") ...
You put the single quote in double quotation marks, try it.

http://www.bkjia.com/PHPjc/896042.html www.bkjia.com true http://www.bkjia.com/PHPjc/896042.html techarticle PHP format timestamp display friendly time, PHP format in the project time is displayed as 2014-10-20 10:22 seems very stiff. In Weibo, QQ space and other sites are usually displayed for a few seconds ago, ...

  • 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.