Home >Backend Development >PHP Tutorial >PHP time calculation function for days and hours

PHP time calculation function for days and hours

WBOY
WBOYOriginal
2016-07-29 09:03:411255browse
<?php
/*
* 计算时间
* @param String $timestamp 需要被减的时间
* @param String $current_time 当前时间戳或者需要计算的时间戳
* @returne String 计算好的时间
*/
function tmspan($timestamp,$current_time=0){
    $timestamp = strtotime($timestamp);
    if(!$current_time) $current_time=time();
    $span=$current_time-$timestamp;
    if($span<60){
        return "刚刚";
    }else if($span<3600){
        return intval($span/60)."分钟前";
    }else if($span<24*3600){
        return intval($span/3600)."小时前";
    }else if($span<(7*24*3600)){
        return intval($span/(24*3600))."天前";
    }else{
        return date('Y-m-d',$timestamp);
    }
 }

We can use this function many times, mainly for calculating article publishing time, comment publishing time, etc.

The above introduces the PHP time calculation function of days and hours, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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