Home > php教程 > php手册 > php格式化时间戳显示友好的时间,php格式化

php格式化时间戳显示友好的时间,php格式化

WBOY
Release: 2016-06-13 09:23:36
Original
1197 people have browsed it

php格式化时间戳显示友好的时间,php格式化

在项目中时间一律显示为2014-10-20 10:22显得很呆板。在微博、QQ空间等网站通常会显示为几秒前,几分钟前,几小时前等容易阅读的时间,我们称之为友好的时间格式。那么用php怎么实现呢?

大体思路如下:

如果是跨年并且大于3天就显示为具体的时间

如果是今天的

        如果是一分钟内则显示几秒之前

        如果是一小时内则显示几分钟前

        如果是当天且大于一小时则显示为几小时前

如果是昨天则显示为昨天几点

如果是前天则显示为前天几点

如果大于三天(没有跨年)则显示为几月几号

 

根据以上思路就不难写出实现代码了:

实现代码如下:

<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

仅供参考,欢迎批评指正或者提供更好的方法。博客地址:http://www.cnblogs.com/dragondean/

 

php date函数可以把时间戳格式化成2012-5-3

date('Y-n-j', time()); // time()函数生成当前时间的时间戳,可以把它换成你自己的时间戳

参考: cn2.php.net/manual/zh/function.date.php
 

PHP 时间格式化

最简单的截取字段 substr("2010-11-23 02:10:45",10)

-------------------
呵呵,因为你存的时候存的不是时间戳,而存的是格式化过的时间,所以没有必要再格式化了;如果你真的要再格式化一次,那就先用strtotime()转成时间戳,然后再date("H_i_s")。。。。。。
你把单引号换成双引号试试
 

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template