php将秒数格式化为多少小时都少分多少秒(时分秒)的字符串形式

 
更多

php将秒数格式化为多少小时都少分多少秒(时分秒)的字符串形式。

/**
 * @param int $second
 * @return string
 */
function formatSecond($second): string {
    $hours = floor($second / 3600);
    $minutes = floor(($second % 3600) / 60);
    $remainingSeconds = $second % 60;

    $timeString = $hours ? $hours . '小时' : '';
    $timeString .= $minutes ? ($timeString ? ' ' : '') . $minutes . '分钟' : '';
    $timeString .= $remainingSeconds ? ($timeString ? ' ' : '') . $remainingSeconds . '秒' : '';

    return $timeString;
}

例如:

// 示例用法  
echo formatSecond(58);       // 输出: 58秒  
echo formatSecond(61);       // 输出: 1分钟1秒  
echo formatSecond(3601);     // 输出: 1小时0分钟1秒
打赏

本文固定链接: https://www.cxy163.net/archives/2961 | 绝缘体

该日志由 绝缘体.. 于 2024年05月27日 发表在 未分类 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: php将秒数格式化为多少小时都少分多少秒(时分秒)的字符串形式 | 绝缘体
关键字: , , , ,

php将秒数格式化为多少小时都少分多少秒(时分秒)的字符串形式:等您坐沙发呢!

发表评论


快捷键:Ctrl+Enter