如何将秒转换为时间格式?
由于某种原因,我将时间格式转换为:03:30 为秒 3*3600 + 30*60,现在
。我想将其转换回其第一个(相同)格式。怎么可能呢?
我的尝试:
3*3600 + 30*60 = 12600
12600 / 60 = 210 / 60 = 3.5, floor(3.5) = 3 = hour
现在,会议记录怎么样?
考虑该值可能类似于 19:00 或 02:51。
我想你已经明白了。
顺便问一下,如何使用 RegEx 将 2:0 转换为 02:00?
For some reason I convert a time format like: 03:30 to seconds 3*3600 + 30*60, now
. I wanna convert it back to its first (same) format up there. How could that be?
My attempt:
3*3600 + 30*60 = 12600
12600 / 60 = 210 / 60 = 3.5, floor(3.5) = 3 = hour
Now, what about the minutes?
Considering the value can be like 19:00 or 02:51.
I think you got the picture.
And by the way, how to convert 2:0 for example to 02:00 using RegEx?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
这可能更简单
gmdate("H:i:s", $seconds)
PHP gmdate
This might be simpler
gmdate("H:i:s", $seconds)
PHP gmdate
如果你想获取时间格式:
If you want to get time format:
如果您知道时间将少于一小时,则可以使用
date()
或$date->format()
函数。这是有效的,因为系统纪元时间从午夜开始(1970 年 1 月 1 日,但这对您来说并不重要)。
如果是一个小时或更长但少于一天,您可以以小时:分钟:秒的格式输出,并带有 `
对于超过一天,您需要使用模数来计算天数,因为这是该纪元的开始日期将变得相关。
希望有帮助。
If the you know the times will be less than an hour, you could just use the
date()
or$date->format()
functions.This works because the system epoch time begins at midnight (on 1 Jan 1970, but that's not important for you).
If it's an hour or more but less than a day, you could output it in hours:mins:secs format with `
For more than a day, you'll need to use modulus to calculate the number of days, as this is where the start date of the epoch would become relevant.
Hope that helps.
也许最简单的方法是:
Maybe the simplest way is:
让
$time
为以秒为单位的时间。现在,小时、分钟和秒分别为
$hours
、$mins
和$seconds
。Let
$time
be the time as number of seconds.Now the hours, minutes and seconds are in
$hours
,$minutes
and$seconds
respectively.另一种解决方案将为您提供传入秒值的天、小时、分钟和秒:
如果预计时间超过一年,则“天”将需要额外的逻辑。使用 str_pad() 或 ltrim() 添加/删除前导零。
Another solution that will give you the days, hours, minutes, and seconds for a passed-in seconds value:
Extra logic will be needed for 'days' if the time is expected to be more than one year. Use str_pad() or ltrim() to add/remove leading zeros.
当您想使用此代码将秒数转换为时间格式(如 小时:分钟:秒)时,ITroubs 答案不会处理剩余的秒数,
这是我处理此问题的方法:
(这还会向一位数分钟和秒添加前导零)
在此示例中 $time 将包含“1:05:21”。
ITroubs answer doesn't deal with the left over seconds when you want to use this code to convert an amount of seconds to a time format like hours : minutes : seconds
Here is what I did to deal with this:
(This also adds a leading zero to one-digit minutes and seconds)
$time will contain "1:05:21" in this example.
如果您要对其进行硬编码,您将按照其他人的建议使用模数来提取时间。
如果您要从 MySQL 数据库返回秒数,假设您的应用程序中不需要秒格式的数据,则有一种更简洁的方法可以做到这一点,您可以使用 MySQL 的 SEC_TO_TIME 它将返回 hh:mm:ss 格式的时间格式。
例如。
If you were to hardcode it you would use modulus to extract the time as others suggested.
If you are returning the seconds from MySQL database, assuming you don't need the data in seconds format in your app, there is a much cleaner way to do it, you can use MySQL's SEC_TO_TIME and it will return time in hh:mm:ss format.
Eg.
抱歉,这已经太晚了,但也许有用
Sorry this is too late but maybe useful
像这样的东西?
摘自:http://www.ckorp.net/sec2time.php
something like this?
grabbed from: http://www.ckorp.net/sec2time.php
使用模:
Use modulo:
仅一个小的附加示例
请求以毫秒为单位的时间
just one small additional example
requested time in miliseconds
这是另一种方法,所有这些都以“0”开头。
这是根据 Flaxious 的答案改编的。
Here is another way with leading '0' for all of them.
It is an adaptation from the answer of Flaxious.
如果您想要好的格式,例如:0:00:00,请使用 str_pad() 作为@Gardner。
If You want nice format like: 0:00:00 use str_pad() as @Gardner.
1 天 = 86400000 毫秒。
解码时间(毫秒/86400000,小时,分钟,秒,毫秒)
Ups!我在想,在delphi中,所有语言中一定有类似的东西。
1 day = 86400000 milliseconds.
DecodeTime(milliseconds/86400000,hr,min,sec,msec)
Ups! I was thinking in delphi, there must be something similar in all languages.