search
Homephp教程PHP源码时间戳友好化格式化函数

在一些微博系统中经常要将时间于现在时间相比显示为多久以前发布的,如显示为:刚刚、5秒前、5小时前、5天前..

/**
 *
 * Description 友好显示时间
 * @param int $time 要格式化的时间戳 默认为当前时间
 * @return string $text 格式化后的时间戳
 * @author yijianqing
 */
 function mdate($time = NULL) {
    $text = '';
    $time = $time === NULL || $time > time() ? time() : intval($time);
    $t = time() - $time; //时间差 (秒)
    if ($t == 0)
        $text = '刚刚';
    elseif ($t < 60)
        $text = $t . &#39;秒前&#39;; // 一分钟内
    elseif ($t < 60 * 60)
        $text = floor($t / 60) . &#39;分钟前&#39;; //一小时内
    elseif ($t < 60 * 60 * 24)
        $text = floor($t / (60 * 60)) . &#39;小时前&#39;; // 一天内
    elseif ($t < 60 * 60 * 24 * 3)
        $text = floor($time/(60*60*24)) ==1 ?&#39;昨天 &#39; . date(&#39;H:i&#39;, $time) : &#39;前天 &#39; . date(&#39;H:i&#39;, $time) ; //昨天和前天
    elseif ($t < 60 * 60 * 24 * 30)
        $text = date(&#39;m月d日 H:i&#39;, $time); //一个月内
    elseif ($t < 60 * 60 * 24 * 365)
        $text = date(&#39;m月d日&#39;, $time); //一年内
    else
        $text = date(&#39;Y年m月d日&#39;, $time); //一年以前
    return $text;
 }

                   


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 Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools