PHP format timestamp displays friendly time, PHP formatting_PHP tutorial

WBOY
Release: 2016-07-13 10:16:48
Original
1080 people have browsed it

PHP formatted timestamp displays a friendly time. PHP formatted

in the project always displays the time as 2014-10-20 10:22, which seems very dull. On websites such as Weibo and QQ Space, the time is usually displayed as a few seconds ago, a few minutes ago, a few hours ago, etc. that are easy to read. We call this a friendly time format. So how to implement it using php?

The general idea is as follows:

If it is a New Year's Eve and it is more than 3 days, it will be displayed as the specific time

If it’s today

If it is within one minute, it will show how many seconds ago it was

If it is within one hour, it will display a few minutes ago

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

If it is yesterday, the time will be displayed as yesterday

If it is the day before yesterday, it will display the time the day before yesterday

If it is more than three days (no New Year span), it will display the day of the month

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

The implementation code is as follows:

<span>//</span><span>格式化友好显示时间</span>
<span>function</span> formatTime(<span>$time</span><span>){
    </span><span>$now</span>=<span>time</span><span>();
    </span><span>$day</span>=<span>date</span>('Y-m-d',<span>$time</span><span>);
    </span><span>$today</span>=<span>date</span>('Y-m-d'<span>);
    
    </span><span>$dayArr</span>=<span>explode</span>('-',<span>$day</span><span>);
    </span><span>$todayArr</span>=<span>explode</span>('-',<span>$today</span><span>);
    
    </span><span>//</span><span>距离的天数,这种方法超过30天则不一定准确,但是30天内是准确的,因为一个月可能是30天也可能是31天</span>
    <span>$days</span>=(<span>$todayArr</span>[0]-<span>$dayArr</span>[0])*365+((<span>$todayArr</span>[1]-<span>$dayArr</span>[1])*30)+(<span>$todayArr</span>[2]-<span>$dayArr</span>[2<span>]);
    </span><span>//</span><span>距离的秒数</span>
    <span>$secs</span>=<span>$now</span>-<span>$time</span><span>;
    
    </span><span>if</span>(<span>$todayArr</span>[0]-<span>$dayArr</span>[0]>0 && <span>$days</span>>3){<span>//</span><span>跨年且超过3天</span>
        <span>return</span> <span>date</span>('Y-m-d',<span>$time</span><span>);
    }</span><span>else</span><span>{
    
        </span><span>if</span>(<span>$days</span><1){<span>//</span><span>今天</span>
            <span>if</span>(<span>$secs</span><60)<span>return</span> <span>$secs</span>.'秒前'<span>;
            </span><span>elseif</span>(<span>$secs</span><3600)<span>return</span> <span>floor</span>(<span>$secs</span>/60)."分钟前"<span>;
            </span><span>else</span> <span>return</span> <span>floor</span>(<span>$secs</span>/3600)."小时前"<span>;
        }</span><span>else</span> <span>if</span>(<span>$days</span><2){<span>//</span><span>昨天</span>
            <span>$hour</span>=<span>date</span>('h',<span>$time</span><span>);
            </span><span>return</span> "昨天".<span>$hour</span>.'点'<span>;
        }</span><span>elseif</span>(<span>$days</span><3){<span>//</span><span>前天</span>
            <span>$hour</span>=<span>date</span>('h',<span>$time</span><span>);
            </span><span>return</span> "前天".<span>$hour</span>.'点'<span>;
        }</span><span>else</span>{<span>//</span><span>三天前</span>
            <span>return</span> <span>date</span>('m月d号',<span>$time</span><span>);
        }
    }
}</span>
Copy after login

For reference only, criticisms and corrections or better methods are welcome. Blog address: http://www.cnblogs.com/dragondean/

php date function can format the timestamp into 2012-5-3

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

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

PHP time formatting

The simplest interception field substr("2010-11-23 02:10:45",10)

------------------
Haha, because when you save, you don’t save the timestamp, but the formatted time, so there is no need to format it again; if you really want to format it again, then use strtotime( ) into a timestamp, and then date("H_i_s"). . . . . .
Try changing the single quotes to double quotes

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/896042.htmlTechArticlephp formatted timestamp displays a friendly time. In php formatted projects, the time is always displayed as 2014-10- 20 10:22 seems very dull. On websites such as Weibo and QQ Space, it is usually displayed as a few seconds ago...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!