Home > Article > Backend Development > How to convert seconds to time format in php
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.
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!