PHP DateTime():显示大于24小时的时间长度,但不得超过24小时

编程入门 行业动态 更新时间:2024-10-25 01:24:27
本文介绍了PHP DateTime():显示大于24小时的时间长度,但不得超过24小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想显示一段时间,以小时,分钟和秒为单位,其中一段时间​​长于24小时。目前我正在尝试:

I would like to display a length of time measured in hours, minutes and seconds where some lengths of time are greater than 24 hours. Currently I am trying this:

$timeLength = new DateTime(); $timeLength->setTime(25, 30); echo $timeLength->format('H:m:i'); // 01:30:00

我希望显示 25: 30:00 。

我正在寻找面向对象的解决方案。

I am looking preferably for an object oriented solution.

谢谢:)

推荐答案

由于你已经有几秒钟的长度,你可以计算它:

Since you already have the length in seconds, you can just calculate it:

function timeLength($sec) { $s=$sec % 60; $m=(($sec-$s) / 60) % 60; $h=floor($sec / 3600); return $h.":".substr("0".$m,-2).":".substr("0".$s,-2); } echo timeLength(6534293); //outputs "1815:04:53"

如果你真的想使用 DateTime 对象,这是一种(一种欺骗)解决方案:

If you really want to use DateTime object, here's a (kind of cheating) solution:

function dtLength($sec) { $t=new DateTime("@".$sec); $r=new DateTime("@0"); $i=$t->diff($r); $h=intval($i->format("%a"))*24+intval($i->format("%H")); return $h.":".$i->format("%I:%S"); } echo dtLength(6534293); //outputs "1815:04:53" too

如果你需要它OO,不介意创建自己的课程,你可以尝试

If you need it OO and don't mind creating your own class, you can try

class DTInterval { private $sec=0; function __construct($s){$this->sec=$sec;} function formet($format) { /*$h=...,$m=...,$s=...*/ $rst=str_replace("H",$h,$format);/*etc.etc.*/ return $rst; } }

更多推荐

PHP DateTime():显示大于24小时的时间长度,但不得超过24小时

本文发布于:2023-10-30 22:53:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544240.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小时   长度   时间   PHP   DateTime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!