search
HomeBackend DevelopmentPHP ProblemHow to calculate how many days ago in php

How to calculate the time a few days ago in php: First create a PHP sample file; then calculate the time a few days ago through the "function time_tran($time){...}" method.

How to calculate how many days ago in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

PHP calculates minutes ago, hours ago, and what time Days ago, a few months ago, a few years ago

/*
* $time 需要格式化的时间戳
* return 格式化时间
*/
 
function time_tran($time){
 
$text = '';
 
if(!$time){return $text;}
 
$current = time();
 
$t = $current - $time;
 
$retArr = array('刚刚','秒前','分钟前','小时前','天前','月前','年前');
 
switch($t){
 
case $t < 0://时间大于当前时间,返回格式化时间
 
$text = date(&#39;Y-m-d&#39;,$time);
 
break;
 
case $t == 0://刚刚
 
$text = $retArr[0];
 
break;
 
case $t < 60:// 几秒前
 
$text = $t.$retArr[1];
 
break;
 
case $t < 3600://几分钟前
 
$text = floor($t / 60).$retArr[2];
 
break;
 
case $t < 86400://几小时前
 
$text = floor($t / 3600).$retArr[3];
 
break;
 
case $t < 2592000: //几天前
 
$text = floor($t / 86400).$retArr[4];
 
break;
 
case $t < 31536000: //几个月前
 
$text = floor($t / 2592000).$retArr[5];
 
break;
 
default : //几年前
 
$text = floor($t / 31536000).$retArr[6];
 
}
 
return $text;
 
}

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to calculate how many days ago in php. For more information, please follow other related articles on the PHP Chinese website!

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment