Output is in seconds. convert to hh:mm:ss format in php

后端 未结 13 1639
礼貌的吻别
礼貌的吻别 2020-12-01 13:54
  1. My output is in the format of 290.52262423327 seconds. How can i change this to 00:04:51?

  2. The same output i want to show in seconds and in HH:MM:SS

相关标签:
13条回答
  • 2020-12-01 14:11

    1)

    $newtime = sprintf( "%02d:%02d:%02d", $time / 3600, $time / 60 % 60, $time % 60 );
    

    2)

    $newsec = sprintf( "%.2f", $time );
    
    0 讨论(0)
  • 2020-12-01 14:12

    Based on https://stackoverflow.com/a/3534705/4342230, but adding days:

    function durationToString($seconds) {
      $time = round($seconds);
    
      return sprintf(
        '%02dD:%02dH:%02dM:%02dS',
        $time / 86400,
        ($time / 3600) % 24,
        ($time / 60) % 60,
        $time % 60
      );
    }
    
    0 讨论(0)
  • 2020-12-01 14:13
    $iSeconds = 290.52262423327;
    print date('H:i:s', mktime(0, 0, $iSeconds));
    
    0 讨论(0)
  • 2020-12-01 14:16

    Try this one

    echo gmdate("H:i:s", 90);
    
    0 讨论(0)
  • 2020-12-01 14:18

    If you're using Carbon (such as in Laravel), you can do this:

    $timeFormatted = \Carbon\Carbon::now()->startOfDay()->addSeconds($seconds)->toTimeString();

    But $timeFormatted = date("H:i:s", $seconds); is probably good enough.

    Just see caveats.

    0 讨论(0)
  • 2020-12-01 14:20

    For till 23:59:59 hours you can use PHP default function

    echo gmdate("H:i:s", 86399);
    

    Which will only return the result till 23:59:59

    If your seconds is more then 86399 than with the help of @VolkerK answer

    $time = round($seconds);
    echo sprintf('%02d:%02d:%02d', ($time/3600),($time/60%60), $time%60);
    

    will be the best options to use ...

    0 讨论(0)
提交回复
热议问题