本文介绍了将秒转换为天,小时,分钟和秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个变量 $ uptime (以秒为单位)转换为天,小时,分钟和秒。

I would like to convert a variable $uptime which is seconds, into days, hours, minutes and seconds.

示例:

$uptime = 1640467;

结果应该是:

18 days 23 hours 41 minutes


推荐答案

这可以通过 class

This can be achieved with DateTime class

使用

echo secondsToTime(1640467);
# 18 days, 23 hours, 41 minutes and 7 seconds

功能:

Function:

function secondsToTime($seconds) {
    $dtF = new \DateTime('@0');
    $dtT = new \DateTime("@$seconds");
    return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
}

这篇关于将秒转换为天,小时,分钟和秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 23:20