Home  >  Article  >  Backend Development  >  How to convert seconds in php to duration

How to convert seconds in php to duration

藏色散人
藏色散人Original
2021-06-18 10:32:442997browse

How to convert seconds to duration in php: 1. Use the "gmstrftime" function to convert seconds within 24 hours; 2. Use the "changeTimeType" method to convert seconds beyond 24 hours.

How to convert seconds in php to duration

The operating environment of this article: Windows 7 system, PHP 7.1 version, DELL G3 computer

How to convert php seconds to duration?

For example: PHP converts seconds (4490) into hours, minutes and seconds (34:02:02)

To convert seconds into hours, minutes and seconds, PHP provides a function gmstrftime, However, this function is limited to converting seconds within 24 hours. How should we display seconds that exceed 24 hours?

For example, 34:02:02

$seconds = 3600*34 + 122;
function changeTimeType($seconds){
    if ($seconds > 3600){
        $hours = intval($seconds/3600);
        $minutes = $seconds % 3600;
        $time = $hours.":".gmstrftime('%M:%S', $minutes);
    }else{
        $time = gmstrftime('%H:%M:%S', $seconds);
    }
    return $time;
}
echo changeTimeType($seconds);

Result output 34:02:02

Recommended Study: "PHP Video Tutorial"

The above is the detailed content of How to convert seconds in php to duration. 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