【问题标题】:Converting seconds to time with two decimal places [duplicate]将秒转换为带两位小数的时间[重复]
【发布时间】:2012-08-29 11:02:09
【问题描述】:

可能重复:
Convert seconds into days, hours, minutes, seconds in PHP
How to convert a decimal into time, eg. HH:MM:SS

我在这里所做的是我在 mysql 中将帖子时间保存为 UTC,在从 sql 检索时间戳之后,我使用strtotime() 将其转换为秒,然后将偏移时间添加到这些秒以显示当前当地时间。现在的问题是,当我尝试将这些秒数重新转换为数小时时,例如

$seconds = 330*60 //Thats 5.30 hours and 60 seconds gives me 19800 as an output

现在,当我将其重新转换回小时数时,它会显示 5.50 而不是 5.30

sprintf ("%.2f", 19800/60/60); //Output of 5.50 instead I want is 5.30 

我知道这是一个数学表达式做错了,如果有任何帮助,我们将不胜感激。

【问题讨论】:

  • 但是330 minutes to hours ?? 5 小时 30 分钟!= 5.30 小时
  • 如果您在脚本中设置了正确的时区,则根本不需要进行该转换
  • 我希望输出为 5.30 而不是 5.50
  • 5个半小时5小时30分钟
  • 哦,我明白了。然后你可以time = 5.5; hours = (int)time; minutes=(time-hours)*60

标签: php datetime mathematical-expressions


【解决方案1】:

不是最优雅的解决方案,但这是可行的:

$original = (19800 / 60 / 60);
$hours = floor($original);
$minutes = ($original - $hours) * 60;

或者使用模数可能会更好:

$hours = floor(19800 / 60 / 60);
$minutes = (19800 / 60) % 60;

【讨论】:

  • 哈哈很好,这很好,还有比这更好的解决方案吗?否则你会得到一个绿色的勾号..
猜你喜欢
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-17
  • 2019-07-04
  • 2014-10-16
  • 1970-01-01
  • 2014-01-29
相关资源
最近更新 更多