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

后端 未结 13 1730
礼貌的吻别
礼貌的吻别 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: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
      );
    }
    

提交回复
热议问题