我有一个函数将时间戳转换为真实的小时,分钟和秒,我会保持格式如下:hh:mm:ss即使它只是几分钟和几秒钟,我应该得到00:10:32而不是10:32:
// format can be either : or blank
function toTime($timestamp, $format)
{
$hours = floor($timestamp / 3600);
$minutes = floor($timestamp % 3600 / 60);
$seconds = $timestamp % 60;
if($format == ':') {
$hourDuration = sprintf('%02d:', $hours);
//echo 'hour: '.$hourDuration.'<br />';
$minDuration = sprintf('%02d:', $minutes);
//echo 'min: '.$minDuration.'<br />';
$secDuration = sprintf('%02d', $seconds);
//echo 'sec: '.$secDuration.'<br />';
$HourMinSec = $hourDuration.$minDuration.$secDuration;
} else {
$hourDuration = sprintf('%02d h', $hours);
//echo 'hour: '.$hourDuration.'<br />';
$minDuration = sprintf('%02d m', $minutes);
//echo 'min: '.$minDuration.'<br />';
$secDuration = sprintf('%02d s', $seconds);
//echo 'sec: '.$secDuration.'<br />';
$HourMinSec = '';
}
if($hourDuration > 0){
$hourDuration = $hourDuration;
} else {
$hourDuration = '';
}
if($minDuration > 0){
$minDuration = $minDuration;
} else {
$minDuration = '';
}
if($secDuration > 0){
$secDuration = $secDuration;
} else {
$secDuration = '';
}
//$HourMinSec = $hourDuration.' '.$minDuration.' '.$secDuration;
$HourMinSec = $hourDuration.$minDuration.$secDuration;
return $HourMinSec;
}
此功能用于获取视频时间戳,如:1508.7397460938,我想整理一下,该视频中有多少小时,分钟和秒
感谢您的协助
答案 0 :(得分:0)
我会这样做的
function toTime($timestamp, $format)
{
$hours = floor($timestamp / 3600);
$minutes = floor($timestamp % 3600 / 60);
$seconds = $timestamp % 60;
if($format == ':') {
$hourDuration = sprintf('%02d:', $hours);
//echo 'hour: '.$hourDuration.'<br />';
$minDuration = sprintf('%02d:', $minutes);
//echo 'min: '.$minDuration.'<br />';
$secDuration = sprintf('%02d', $seconds);
//echo 'sec: '.$secDuration.'<br />';
$HourMinSec = $hourDuration.$minDuration.$secDuration;
} else {
$hourDuration = sprintf('%02d h', $hours);
//echo 'hour: '.$hourDuration.'<br />';
$minDuration = sprintf('%02d m', $minutes);
//echo 'min: '.$minDuration.'<br />';
$secDuration = sprintf('%02d s', $seconds);
//echo 'sec: '.$secDuration.'<br />';
$HourMinSec = '';
}
if($hourDuration > 0){
$hourDuration = $hourDuration;
} else {
$hourDuration = ($format == ':' ? '00:' : '00 h');
}
if($minDuration > 0){
$minDuration = $minDuration;
} else {
$minDuration = ($format == ':' ? '00:' : '00 m');
}
if($secDuration > 0){
$secDuration = $secDuration;
} else {
$secDuration = '00';
}
//$HourMinSec = $hourDuration.' '.$minDuration.' '.$secDuration;
$HourMinSec = $hourDuration.$minDuration.$secDuration;
return $HourMinSec;
}
在这种情况下,它会为您带来以下添加
的结果echo toTime(1508.7397460938);
Output --> 00 h25 m08 s
echo toTime(1508.7397460938,':');
Output --> 00:25:08
另一种变体是通过键入:
来使用php ready函数gmdateecho gmdate("H:i:s", 1508.7397460938);
//仅在小时数小于24时使用该功能,该功能现在显示超过24小时,如果是,则显示不准确的数据。