如何使用 PHP gmdate 函数将 00:00:23 转换为 00:23


How to convert 00:00:23 to 00:23 using PHP gmdate function

>我有此代码在我的应用程序中显示视频持续时间。

$duration = 23; // seconds
$duration = gmdate("H:i:s", $duration);
上面的代码输出 00:00:23,但小时字段不是必需的,因为它只有 23 秒,

我希望它显示 00:23 秒,例如,如果视频长度超过小时,它应该显示 01:24:19

知道吗?

谢谢

试试这个:

$duration = gmdate($duration >= 3600 ? 'H:i:s' : 'i:s', $duration);
上面的代码输出 00:00:23,但小时字段不是必需的,因为它只有 23 秒,

我希望它显示 00:23 秒,例如,如果视频长度超过小时,它应该显示 01:24:19

一个简单的如果怎么样?

<?php
$duration = $duration >= ( 60 * 60 ) ? gmdate( 'H:i:s', $duration ) : gmdate( 'i:s', $duration );
echo $duration;
$duration = gmdate( ($duration >= 3600 ? "H:i:s" : "i:s"), $duration);