简体   繁体   English

将时间格式从04:00更改为00:04:00

[英]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.

相关问题 从PHP获取当前时间/日期,格式类似于2012-04-24T00:00:00Z - get current time / date from php in format similar to 2012-04-24T00:00:00Z 如何将“ 00:04:13.67”格式的时间转换为秒? - How do I convert time in the format '00:04:13.67' to seconds? new DateTime('2016-04-01 00:00:00')返回'2016-04-01 12:00:00 - new DateTime('2016-04-01 00:00:00') returns '2016-04-01 12:00:00 格式datetime(0000-00-00 00:00:00)到00:00:00并保留时间 - format datetime(0000-00-00 00:00:00 ) to 00:00:00 with time remaing 这是什么日期格式? 2021-10-04T08:19:54.000+04:00 - What is this date format? 2021-10-04T08:19:54.000+04:00 php日期从09:04:00持续计数到09:04:59 - Php date keeps counting from 09:04:00 to 09:04:59 php 2021-07-09T14:09:47.529751-04:00 什么样的数据时间格式以及如何处理 - What kind of data time format and how to process this in php 2021-07-09T14:09:47.529751-04:00 如何将数据格式“ 27/04/2014 00:40”传递给“ 2014年4月27日,星期日”? - How I can pass this data format “27/04/2014 00:40” to this: “Sunday, 27 April, 2014”? 将字符串中的给定时间(根据客户端时区为04:50:00)转换为PHP中的UTC时间 - Turn given time in string (04:50:00 according to clients timezone) into UTC time in PHP 如果字符串是格式为00:00的时间,则PHP获取 - PHP get if string is a time in the format 00:00
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM