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

PHP中文网
Release: 2023-02-28 15:36:02
Original
2911 people have browsed it

一. 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;
}
Copy after login

二. 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;
}
Copy after login

相关文章:

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

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!