search
Homephp教程php手册根据刚刚,1分钟前,1小时前,几天前显示文章发布日期

一分钟之内发布的,显示“刚刚”
一小时之内发布的,显示“XX分钟前”
一个月之内发布的,显示“XX天前”
一个月外的直接显示日期。

代码收藏:

<span //</span><span 发表于几分钟前</span>
<span function</span> kele_time_since(<span $older_date</span>, <span $newer_date</span> = <span false</span><span )
{
    </span><span $chunks</span> = <span array</span><span (
        </span><span array</span>(60 * 60 * 24 * 365 , '年'),
        <span array</span>(60 * 60 * 24 * 30 , '月'),
        <span //</span><span array(60 * 60 * 24 * 7, '周'),</span>
        <span array</span>(60 * 60 * 24 , '天'),
        <span array</span>(60 * 60 , '小时'),
        <span array</span>(60 , '分钟'),<span 
    );
 
    </span><span $newer_date</span> = (<span $newer_date</span> == <span false</span>) ? (<span time</span>()+(60*60*get_settings("gmt_offset"))) : <span $newer_date</span><span ;
    </span><span $since</span> = <span $newer_date</span> - <span abs</span>(<span strtotime</span>(<span $older_date</span><span ));
 
    </span><span //</span><span 根据自己的需要调整时间段,下面的24则表示小时,根据需要调整吧</span>
    <span $chunks_count</span>=<span count</span>(<span $chunks</span><span );
    </span><span if</span>(<span $since</span> < 60 * 60 * 24*30<span ){
        </span><span for</span> (<span $i</span> = 0, <span $j</span> = <span $chunks_count</span>; <span $i</span> < <span $j</span>; <span $i</span>++<span ){
            </span><span $seconds</span> = <span $chunks</span>[<span $i</span>][0<span ];
            </span><span $name</span> = <span $chunks</span>[<span $i</span>][1<span ];
 
            </span><span if</span> ((<span $count</span> = <span floor</span>(<span $since</span> / <span $seconds</span>)) != 0<span ){
                </span><span break</span><span ;
            }
        }
        </span><span $out</span> = (<span $count</span> == 1) ? '1'.<span $name</span> : "<span $count</span>{<span $name</span>}"<span ;
        </span><span if</span>(0==<span $count</span><span ){
            </span><span return</span> "刚刚"<span ;
        }
        </span><span return</span> <span $out</span>."前"<span ;
    }</span><span else</span><span {
        the_time(get_option(</span>'date_format'<span ));
    }
}</span>

(原文:http://www.dayecn.com/tanteng/article/show-date/)

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),