简体   繁体   English

PHP DateInterval格式分钟和秒,前导零

[英]PHP DateInterval format minutes and seconds with leading zero

Trying to generate simple "H:i:s" string from seconds, but can't 试图从几秒钟生成简单的“ H:i:s”字符串,但无法

example: 例:

$iSecond = 188;
$dtF = new DateTime("@0");
$dtT = new DateTime("@$iSecond");
$size = $dtF->diff($dtT)->format('%H:%i:%s');
echo $size;

should show "00:03:08" , but in fact "00:3:8" 应该显示“ 00:03:08”,但实际上是“ 00:3:8”

if there is some another date format options to use in format method? 是否还有其他日期格式选项可用于格式化方法?

there is fiddle https://3v4l.org/Coi7q 有小提琴https://3v4l.org/Coi7q

Use capital I and S for your formatting: 使用大写字母IS进行格式化:

$size = $dtF->diff($dtT)->format('%H:%I:%S');

See the manual for all of the formatting options 有关所有格式选项,请参见手册

Demo 演示版

John is correct, but your code seems unnecessary, this should be the same: John是正确的,但是您的代码似乎不必要,这应该是相同的:

$iSecond = 188;
$dtT = new DateTime("@$iSecond");
$size = $dtT->format('H:i:s');

There is no reason to create a datetime of @0 and use diff/DateInterval in this code. 没有理由在此代码中创建日期时间@ 0并使用diff / DateInterval。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM