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

How to convert seconds to time format in php

藏色散人
Release: 2023-03-17 14:12:01
Original
2001 people have browsed it

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;
    }
Copy after login

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!

Related labels:
php
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template