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

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

WBOY
WBOYOriginal
2016-06-06 20:08:561533browse

工作当中有时会要用到时间转换成几月前,几周前,几天前,几小时前,几分钟前,几秒前这样样显示结构方式,下面写了个通用方法保留一下. /** * 时间转换 * @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.'秒前';
        }
}

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