Home > Backend Development > PHP Tutorial > Friendly display time on PHP side

Friendly display time on PHP side

WBOY
Release: 2016-07-28 08:26:17
Original
1126 people have browsed it

<code><span>/**
     * 显示某一个时间相当于当前时间在多少秒前,多少分钟前,多少小时前, 如果超过1年, 就直接显示具体时间
     *
     *<span> @param</span> int    $inputTimestamp UnixTimestamp
     *<span> @param</span> string $overflowTimeFormat 超过3天的时间显示格式
     *
     *<span> @return</span> string
     */</span><span>static</span><span>public</span><span><span>function</span><span>timeAgo</span><span>(<span>$inputTimestamp</span>,<span>$overflowTimeFormat</span> = <span>'Y/m/d H:i'</span>)</span>
    {</span><span>if</span> (<span>empty</span>(<span>$inputTimestamp</span>) || !is_numeric(<span>$inputTimestamp</span>) || !<span>$inputTimestamp</span>) {
            <span>return</span><span>''</span>;
        }
        <span>$d</span> = time() - <span>$inputTimestamp</span>;
        <span>if</span> (<span>$d</span> < <span>0</span>) {
            <span>return</span><span>''</span>;
        } <span>else</span> {
            <span>if</span> (<span>$d</span> < <span>60</span>) {
                <span>return</span><span>$d</span> . <span>'秒前'</span>;
            } <span>else</span> {
                <span>if</span> (<span>$d</span> < <span>3600</span>) {
                    <span>return</span> floor(<span>$d</span> / <span>60</span>) . <span>'分钟前'</span>;
                } <span>else</span> {
                    <span>if</span> (<span>$d</span> < <span>86400</span>) {
                        <span>return</span> floor(<span>$d</span> / <span>3600</span>) . <span>'小时前'</span>;
                    } <span>else</span> {
                        <span>if</span> (<span>$d</span> < <span>259200</span>) {<span>//3天内</span><span>return</span> floor(<span>$d</span> / <span>86400</span>) . <span>'天前'</span>;
                        } <span>else</span> {
                            <span>return</span> date(<span>$overflowTimeFormat</span>,<span>$inputTimestamp</span>);
                        }
                    }
                }
            }
        }
    }</code>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above has introduced the friendly display time PHP side, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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