如何将秒转换为时间格式? [英] How to convert seconds to time format?

查看:189
本文介绍了如何将秒转换为时间格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,我将时间格式转换为: 03:30至秒3 * 3600 + 30 * 60,现在。我想把它转换回它的第一个(相同)格式。

我的尝试:

  3 * 3600 + 30 * 60 = 12600 

12600/60 = 210/60 = 3.5,floor(3.5)= 3 =小时

现在,分钟呢?

考虑到价值可以像 19:00或02:51。
我想你有



顺便说一下,如何使用RegEx将 2:0转换为02:00?

解决方案

$ hours = floor($ seconds / 3600);
$ mins = floor($秒/ 60%60);
$ secs = floor($ seconds%60);

如果您想获取时间格式:

  $ timeFormat = sprintf('%02d:%02d:%02d',$ hours,$ mins,$ secs); 


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?

解决方案

$hours = floor($seconds / 3600);
$mins = floor($seconds / 60 % 60);
$secs = floor($seconds % 60);

If you want to get time format:

$timeFormat = sprintf('%02d:%02d:%02d', $hours, $mins, $secs);

这篇关于如何将秒转换为时间格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆