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

后端 未结 13 1731
礼貌的吻别
礼貌的吻别 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:10

    Personally, going off other peoples answers I made my own parser. Works with days, hours, minutes and seconds. And should be easy to expand to weeks/months etc. It works with deserialisation to c# as well

    function secondsToTimeInterval($seconds) {
        $t = round($seconds);
        $days = floor($t/86400);
        $day_sec = $days*86400;
        $hours = floor( ($t-$day_sec) / (60 * 60) );
        $hour_sec = $hours*3600;
        $minutes = floor((($t-$day_sec)-$hour_sec)/60);
        $min_sec = $minutes*60;
        $sec = (($t-$day_sec)-$hour_sec)-$min_sec;
        return sprintf('%02d:%02d:%02d:%02d', $days, $hours, $minutes, $sec);
    }
    

提交回复
热议问题