[英]Change time format from 04:00 to 00:04:00
I currently get duration in this format: 04:00
but I need in this format 00:04:00
. 目前,我的持续时间为以下格式:
04:00
: 04:00
但我需要的格式为00:04:00
。
I am using this code but it's not working correctly. 我正在使用此代码,但无法正常工作。
$time = date("h:i:s", strtotime($duration));
echo $time;
这样尝试
$duration = "00:"."04:00";
This should work for you: 这应该为您工作:
(Your problem is in which format you read in your time, so DateTime::createFromFormat
) (您的问题是您在读时所用的格式,因此
DateTime::createFromFormat
)
$date = DateTime::createFromFormat("i:s", "04:00");
echo $date->format('H:i:s');
Output: 输出:
00:04:00
The problem is the strtotime($duration)
; 问题是
strtotime($duration)
; It doesn't know exactly what format that's in, and can't get a valid date from it. 它不知道确切的格式,也无法从中获取有效的日期。 (This is why you're getting unexpected results from the
date
function. (这就是为什么您从
date
函数中获得意外结果的原因。
If you want to just simply add 00:
before $duration
, you can do it like so: 如果只想在
$duration
之前添加00:
$duration
可以这样:
$duration = '04:00';
$duration = '00:' . $duration;
You then would be able to pass this to the strtotime
function, and would likely get better results. 然后,您可以将其传递给
strtotime
函数,并且可能会获得更好的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.