Home > php教程 > php手册 > php时间转换成(月,周,天,小时,分钟,秒)通用方法

php时间转换成(月,周,天,小时,分钟,秒)通用方法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:08:56
Original
1588 people have browsed it

工作当中有时会要用到时间转换成几月前,几周前,几天前,几小时前,几分钟前,几秒前这样样显示结构方式,下面写了个通用方法保留一下. /** * 时间转换 * @param type $timestamp * @return type*/function humandate($timestamp) { $seconds = time() - $timesta

工作当中有时会要用到时间转换成几月前,几周前,几天前,几小时前,几分钟前,几秒前这样样显示结构方式,下面写了个通用方法保留一下.

/**
 *  时间转换
 * @param type $timestamp
 * @return type
*/
function humandate($timestamp) {
        $seconds = time() - $timestamp;
        if($seconds > 31536000) {
                return date('Y-n-j',$timestamp);
        } elseif($seconds > 2592000) {
                return ceil($seconds / 2592000).'月前';
        } elseif($seconds > 86400) {
                return ceil($seconds / 86400).'天前';
        } elseif($seconds > 3600) {
                return ceil($seconds / 3600).'小时前';
        } elseif($seconds > 60) {
                return ceil($seconds / 60).'分钟前';
        } else {
                return $seconds.'秒前';
        }
}
Copy after login

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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template