Forked from bcls/convert_milliseconds_to_formatted_time.php
Created
March 5, 2020 21:57
-
-
Save threepointnz/50450f6694e3812a5f5931f7daa87dc5 to your computer and use it in GitHub Desktop.
convert milliseconds to formatted time #php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Converts milliseconds to formatted time or seconds. | |
* @param int [$ms] The length of the media asset in milliseconds | |
* @param bool [$seconds] Whether to return only seconds | |
* @return mixed The formatted length or total seconds of the media asset | |
*/ | |
function convertTime($ms, $seconds = false) | |
{ | |
$total_seconds = ($ms / 1000); | |
if($seconds) | |
{ | |
return $total_seconds; | |
} else { | |
$time = ''; | |
$value = array( | |
'hours' => 0, | |
'minutes' => 0, | |
'seconds' => 0 | |
); | |
if($total_seconds >= 3600) | |
{ | |
$value['hours'] = floor($total_seconds / 3600); | |
$total_seconds = $total_seconds % 3600; | |
$time .= $value['hours'] . ':'; | |
} | |
if($total_seconds >= 60) | |
{ | |
$value['minutes'] = floor($total_seconds / 60); | |
$total_seconds = $total_seconds % 60; | |
$time .= $value['minutes'] . ':'; | |
} else { | |
$time .= '00:'; | |
} | |
$value['seconds'] = floor($total_seconds); | |
if($value['seconds'] < 10) | |
{ | |
$value['seconds'] = '0' . $value['seconds']; | |
} | |
$time .= $value['seconds']; | |
return $time; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment