Home>Article>Backend Development> php convert seconds to hours minutes seconds

php convert seconds to hours minutes seconds

Mary-Kate Olsen
Mary-Kate Olsen Original
2021-10-14 11:00:53 5462browse

php method to convert seconds into hours, minutes and seconds: 1. Create a PHP sample file; 2. Convert seconds into seconds by creating the "function secondChanage($second = 0){...}" method Hours, minutes and seconds will do.

php convert seconds to hours minutes seconds

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

php method to convert seconds into hours, minutes and seconds :

Preface

One method that needs to be used for communication records is to convert seconds into hours, minutes and seconds

Method

PHP has a built-in method, you can use it directly, but this is only within 24 hours. It is enough for the address book~

Example

$v = 30;gmdate('H:i:s', $v); //00:00:30

If you want to change it to XX hours, XX minutes and XX seconds, this All you need to do is convert the format. I directly use ternary nesting here

/**
* 处理时间
*
* @param string $s 转化好的时间
*
* @return string $ftime 处理好的时间 */
public function ftime($s)
{
$time = explode(':', $s);
$time['0'] == '00' ? $h = 0 : $h = $time['0'];
$time['1'] == '00' ? $m = 0 : $m = $time['1'];
$time['2'] == '00' ? $s = 0 : $s = $time['2'];
$ftime = (
empty($h) ? (
empty($m) ? $s .'秒' : (
empty($s) ? $m . '分' : $m . '分' . $s .'秒'
)
) :(
empty($m) && empty($s) ? $h .'小时' : (
empty($m) ? $h . '时' . $s . '秒' : (
empty($s) ? $h . '小时' . $m . '分' : $h . '小时' . $m . '分' .$s . '秒'
)
)
)
); return $ftime;
}

Rendering

## Recommended learning: "

PHP Video tutorial

But this is only within 24 hours. Although it is enough for the address book, what if you need to display the day next time? So next, write a new method.

/**
* 秒转换为天,小时,分钟
*
* @param int $second 时间戳
*
* @return string */
function secondChanage($second = 0)
{
$newtime = '';
$d = floor($second / (3600*24));
$h = floor(($second % (3600*24)) / 3600);
$m = floor((($second % (3600*24)) % 3600) / 60);
$s = $second - ($d*24*3600) - ($h*3600) - ($m*60); empty($d) ?
$newtime = (
empty($h) ? (
empty($m) ? $s . '秒' : (

empty($s) ? $m.'分' : $m.'分'.$s.'秒'
)
) : (
empty($m) && empty($s) ? $h . '时' : (
empty($m) ? $h . '时' . $s . '秒' : (
empty($s) ? $h . '时' . $m . '分' : $h . '时' . $m . '分' . $s . '秒'
)
)
)
) : $newtime = (
empty($h) && empty($m) && empty($s) ? $d . '天' : (
empty($h) && empty($m) ? $d . '天' . $s .'秒' : (
empty($h) && empty($s) ? $d . '天' . $m .'分' : (
empty($m) && empty($s) ? $d . '天' .$h . '时' : (
empty($h) ? $d . '天' .$m . '分' . $s .'秒' : (
empty($m) ? $d . '天' .$h . '时' . $s .'秒' : (
empty($s) ? $d . '天' .$h . '时' . $m .'分' : $d . '天' .$h . '时' . $m .'分' . $s . '秒'
)
)
)
)
)
)
);
return $newtime;

}

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