美文网首页
php - 秒的转换 (年 天 小时 分钟 秒) (天 小时 分

php - 秒的转换 (年 天 小时 分钟 秒) (天 小时 分

作者: 阿然学编程 | 来源:发表于2022-05-30 15:54 被阅读0次

php - 秒数的转换 (年 天 小时 分钟 秒)

/**
 *秒数转换:年 天 小时 分钟 秒
 * @param $second
 * @return bool|string
 */
function second_time($second)
{
    if (!is_numeric($second)) {
        return false;
    }
    $dataTime = array(
        "years" => 0,
        "days" => 0,
        "hours" => 0,
        "minutes" => 0,
        "seconds" => 0,
    );

    if ($second >= 31556926) {
        $dataTime["years"] = floor($second / 31556926);

        $second = ($second % 31556926);

    }

    if ($second >= 86400) {
        $dataTime["days"] = floor($second / 86400);

        $second = ($second % 86400);

    }

    if ($second >= 3600) {
        $dataTime["hours"] = floor($second / 3600);

        $second = ($second % 3600);

    }

    if ($second >= 60) {
        $dataTime["minutes"] = floor($second / 60);

        $second = ($second % 60);

    }

    $dataTime["seconds"] = floor($second);

    $time = $dataTime["years"] . "年" . $dataTime["days"] . "天" . " " . $dataTime["hours"] . "小时" . $dataTime["minutes"] . "分" . $dataTime["seconds"] . "秒";

    return $time;
}

php - 秒数转换 (天 小时 分钟 秒) (小时 分钟 秒)

/**
 * 秒数转换:天 小时 分钟 秒
 * @param $second
 * @return string
 */
function convert($second)
{
    $newtime = '';
    $d = floor($second / (3600 * 24));
    $h = floor(($second % (3600 * 24)) / 3600);
    $m = floor((($second % (3600 * 24)) % 3600) / 60);
    $s = floor($second % 60);

    if ($d > '0') {
        if ($h == '0' && $m == '0') {
            $newtime = $d . '天';
        } else {
            $newtime = $d . '天' . $h . '小时' . $m . '分钟' . $s . '秒';
        }
    } else {
        if ($h != '0') {
            if ($m == '0') {
                $newtime = $h . '小时';
            } else {
                $newtime = $h . '小时' . $m . '分' . $s . '秒';
            }
        } else {
            $newtime = $m . '分' . $s . '秒';
        }
    }
    return $newtime;
}

相关文章

网友评论

      本文标题:php - 秒的转换 (年 天 小时 分钟 秒) (天 小时 分

      本文链接:https://www.haomeiwen.com/subject/mkuzprtx.html