【问题标题】:PHP; time conversion form distance and speedPHP;时间转换形式距离和速度
【发布时间】:2020-11-14 05:23:54
【问题描述】:

我在转换从速度和距离获得的时间时遇到问题,这是我的代码;

$dist = 30; // Distance which is in kilometres(km)
// speed is in knots(kt), if I take speed of 40kt and convert it into kilometres(km/h) ... 40 * 1.852 = 74.08
$time = ($dist / 74.08) / 24;

// result is 0.016873650107991 which is correct, but my problem is how can this be format in H:m:s,
// tried with date('h:m:s', strtotime($time) but result is always 01:01:00 no mater the distance
echo $time;

任何想法如何将时间格式化为 H:m:s 或改进此代码? 我已经搜索堆栈但没有发现类似的问题,如果我错过了重复抱歉,但链接非常受欢迎。

【问题讨论】:

  • 时间格式以秒为单位消耗0.016873650107991。您需要使用h:i:s
  • 也试过了,还是一样.. 01:00:00
  • 您的具体问题是什么?

标签: php datetime-conversion


【解决方案1】:

如果小时数 > 24,这也将涵盖这种情况

$dist = 10; // Distance which is in kilometres(km)

$ts = ($dist / 74.08) * 3600; // in seconds

$h = floor($ts/3600);
$m = floor(($ts / 60) % 60);
$s = $ts % 60;

echo "$h:$m:$s";

【讨论】:

    【解决方案2】:

    这可能会有所帮助,

    $dist = 10; // Distance which is in kilometres(km)
    // speed is in knots(kt), if I take speed of 40kt and convert it into kilometres(km/h) ... 40 * 1.852 = 74.08
    $time = ($dist / 74.08);
    
    echo gmdate("H:i:s", $time * 3600);
    

    【讨论】:

    • 谢谢你,正是我需要的!
    • 请注意,gmdate() 对于长距离或高速不可靠,因为作为 date 函数,它假定您的小时和分钟不是持续时间而是一天中的时间, 从而计算结果的 24 模。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    相关资源
    最近更新 更多