PHP把秒转换成天数,小时数和分钟格式时间

PHP中文网
发布: 2023-02-28 15:36:02
原创
2912 人浏览过

一. PHP把秒转换成天数,小时数和分钟格式时间简单写法

function secsToStr($secs) {
    if($secs>=86400){$days=floor($secs/86400);$secs=$secs%86400;$r=$days.&#39; day&#39;;if($days<>1){$r.=&#39;s&#39;;}if($secs>0){$r.=&#39;, &#39;;}}
    if($secs>=3600){$hours=floor($secs/3600);$secs=$secs%3600;$r.=$hours.&#39; hour&#39;;if($hours<>1){$r.=&#39;s&#39;;}if($secs>0){$r.=&#39;, &#39;;}}
    if($secs>=60){$minutes=floor($secs/60);$secs=$secs%60;$r.=$minutes.&#39; minute&#39;;if($minutes<>1){$r.=&#39;s&#39;;}if($secs>0){$r.=&#39;, &#39;;}}
    $r.=$secs.&#39; second&#39;;if($secs<>1){$r.=&#39;s&#39;;}
    return $r;
}
登录后复制

二. PHP把小于多少秒转换成天数,小时数和分钟格式时间完整写法

/**
 * 时间友好型提示风格化(即微博中的XXX小时前、昨天等等)
 *
 * 即微博中的 XXX 小时前、昨天等等, 时间超过 $time_limit 后返回按 out_format 的设定风格化时间戳
 *
 * @param  int
 * @param  int
 * @param  string
 * @param  array
 * @param  int
 * @return string
 */
function date_friendly($timestamp, $time_limit = 604800, $out_format = &#39;Y-m-d H:i&#39;, $formats = null, $time_now = null)
{
    if (get_setting(&#39;time_style&#39;) == &#39;N&#39;)
    {
        return date($out_format, $timestamp);
    }
    if (!$timestamp)
    {
        return false;
    }
    if ($formats == null)
    {
        $formats = array(&#39;YEAR&#39; => AWS_APP::lang()->_t(&#39;%s 年前&#39;), &#39;MONTH&#39; => AWS_APP::lang()->_t(&#39;%s 月前&#39;), &#39;DAY&#39; => AWS_APP::lang()->_t(&#39;%s 天前&#39;), &#39;HOUR&#39; => AWS_APP::lang()->_t(&#39;%s 小时前&#39;), &#39;MINUTE&#39; => AWS_APP::lang()->_t(&#39;%s 分钟前&#39;), &#39;SECOND&#39; => AWS_APP::lang()->_t(&#39;%s 秒前&#39;));
    }
    $time_now = $time_now == null ? time() : $time_now;
    $seconds = $time_now - $timestamp;
    if ($seconds == 0)
    {
        $seconds = 1;
    }
    if (!$time_limit OR $seconds > $time_limit)
    {
        return date($out_format, $timestamp);
    }
    $minutes = floor($seconds / 60);
    $hours = floor($minutes / 60);
    $days = floor($hours / 24);
    $months = floor($days / 30);
    $years = floor($months / 12);
    if ($years > 0)
    {
        $diffFormat = &#39;YEAR&#39;;
    }
    else
    {
        if ($months > 0)
        {
            $diffFormat = &#39;MONTH&#39;;
        }
        else
        {
            if ($days > 0)
            {
                $diffFormat = &#39;DAY&#39;;
            }
            else
            {
                if ($hours > 0)
                {
                    $diffFormat = &#39;HOUR&#39;;
                }
                else
                {
                    $diffFormat = ($minutes > 0) ? &#39;MINUTE&#39; : &#39;SECOND&#39;;
                }
            }
        }
    }
    $dateDiff = null;
    switch ($diffFormat)
    {
        case &#39;YEAR&#39; :
            $dateDiff = sprintf($formats[$diffFormat], $years);
            break;
        case &#39;MONTH&#39; :
            $dateDiff = sprintf($formats[$diffFormat], $months);
            break;
        case &#39;DAY&#39; :
            $dateDiff = sprintf($formats[$diffFormat], $days);
            break;
        case &#39;HOUR&#39; :
            $dateDiff = sprintf($formats[$diffFormat], $hours);
            break;
        case &#39;MINUTE&#39; :
            $dateDiff = sprintf($formats[$diffFormat], $minutes);
            break;
        case &#39;SECOND&#39; :
            $dateDiff = sprintf($formats[$diffFormat], $seconds);
            break;
    }
    return $dateDiff;
}
登录后复制

相关文章:

PHP把秒转换成天数,小时数和分钟

js中的时间转换—毫秒转换成日期时间的示例代码

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板