Skip to content

Instantly share code, notes, and snippets.

@Debashis-Sinha
Created July 16, 2017 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Debashis-Sinha/2749672783c59393c956e18d48203fee to your computer and use it in GitHub Desktop.
Save Debashis-Sinha/2749672783c59393c956e18d48203fee to your computer and use it in GitHub Desktop.
php seconds to time
/**
* Seconds to time
* @param $sec
* @return string
*/
static function secondsToTime( $sec ){
if( !is_float( $sec ) ) $sec = floatval( $sec );
$hours = floor($sec / 3600);
$minutes = floor(($sec - ($hours * 3600)) / 60);
$seconds = $sec - ($hours * 3600) - ($minutes * 60);
if ( $hours < 10 ) $hours = "0" . $hours;
if ( $minutes < 10 ) $minutes = "0" . $minutes;
if ( $seconds < 10 ) $seconds = "0" . $seconds;
$seconds = number_format( $seconds, 2, '.', '' );
return $hours . ':' . $minutes . ':' . $seconds;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment