Home  >  Article  >  Backend Development  >  How to convert seconds to hours, minutes and seconds in php

How to convert seconds to hours, minutes and seconds in php

藏色散人
藏色散人Original
2021-09-02 09:33:343914browse

php method to convert seconds into hours, minutes and seconds: 1. Convert seconds into hours, minutes and seconds through the "function changeTimeType($seconds){...}" method; 2. Through "function Sec2Time($ time){...}" method converts seconds into days, hours, minutes and seconds.

How to convert seconds to hours, minutes and seconds in php

The operating environment of this article: Windows7 system, PHP7.1 version, Dell G3 computer

How to convert seconds into hours and minutes in php Second?

PHP converts seconds into hours, minutes and seconds

One:

/**
 * 将秒数转换成时分秒
 *
 * @param 秒数 $seconds
 * @return void
 */
function changeTimeType($seconds)
{
    if ($seconds > 3600) {
        $hours = intval($seconds / 3600);
        $time = $hours . ":" . gmstrftime('%M:%S', $seconds);
    } else {
        $time = gmstrftime('%H:%M:%S', $seconds);
    }
    return $time;
}

Two:

/**
 * 转换成 年 天 时 分 秒
 *
 * @param [type] $time
 * @return void
 */
function Sec2Time($time)
{
    if (is_numeric($time)) {
        $value = array(
            "years" => 0, "days" => 0, "hours" => 0,
            "minutes" => 0, "seconds" => 0,
        );
        $t = '';
        if ($time >= 31556926) {
            $value["years"] = floor($time / 31556926);
            $time = ($time % 31556926);
            $t .= $value["years"] . "年";
        }
        if ($time >= 86400) {
            $value["days"] = floor($time / 86400);
            $time = ($time % 86400);
            $t .= $value["days"] . "天";
        }
        if ($time >= 3600) {
            $value["hours"] = floor($time / 3600);
            $time = ($time % 3600);
            $t .= $value["hours"] . "小时";
        }
        if ($time >= 60) {
            $value["minutes"] = floor($time / 60);
            $time = ($time % 60);
            $t .= $value["minutes"] . "分";
        }
        $value["seconds"] = floor($time);
        //return (array) $value;
        $t .= $value["seconds"] . "秒";
        return $t;
    } else {
        return (bool) false;
    }
}

Recommended study:《PHP video tutorial

The above is the detailed content of How to convert seconds to hours, minutes and seconds in php. For more information, please follow other related articles on the PHP Chinese website!

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