Home >Backend Development >PHP Problem >How to convert seconds to time format in php

How to convert seconds to time format in php

藏色散人
藏色散人Original
2022-11-21 09:38:141959browse

php method to convert seconds into time format: 1. Create a php sample file; 2. Define a "private function convert($second){...}" method; 3. Pass it in the method body "floor($second / (3600*24));" and other formulas can convert seconds into time.

How to convert seconds to time format in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to convert seconds to time format in php

php- Seconds conversion (days, hours, minutes) (hours and minutes)

php Seconds to Days hours minutes hours minutes

The code is as follows:

private function convert($second)
    {
        $newtime = '';
        $d = floor($second / (3600*24));
        $h = floor(($second % (3600*24)) / 3600);
        $m = floor((($second % (3600*24)) % 3600) / 60);
        if ($d>'0') {
            if ($h == '0' && $m == '0') {
                $newtime= $d.'天';
            } else {
                $newtime= $d.'天'.$h.'小时'.$m.'分钟';
            }
        } else {
            if ($h!='0') {
                if ($m == '0') {
                    $newtime= $h.'小时';
                } else {
                    $newtime= $h.'小时'.$m.'分';
                }
            } else {
                $newtime= $m.'分';
            }
        }
        return $newtime;
    }

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert seconds to time format 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