我不认为 PHP 是正确的工具。如果您想在屏幕上为您的用户显示它,听起来 Javascript 可能就是您所追求的。
对于 PHP,可以使用 date 函数
function secondsToWebvtt($seconds) {
//set the time to midnight (the actual date part is inconsequential)
$time = mktime(0,0,0,1,2,2012);
//add the number of seconds
$time+= $seconds;
//return the time in hh:mm:ss.000 format
return date("H:i:s.000",$time);
}
使用 Javascript,我会使用这样的函数
var seconds = 0;
function toTime() {
var time = new Date("1/1/2012 0:00:00");
var newSeconds = time.getSeconds() + seconds;
var strSeconds = newSeconds + "";
if(strSeconds.length < 2) { strSeconds = "0" + strSeconds; }
var hours = time.getHours() + "";
if(hours.length < 2) { hours = "0" + hours; }
var minutes = time.getMinutes() + "";
if(minutes.length < 2) { minutes = "0" + minutes; }
var dispTime = hours + ":" + minutes + ":" + strSeconds + ".000";
return dispTime;
}
function getTime() {
var time = toTime(seconds);
//do something with time here, like displaying on the page somewhere.
seconds++;
}
然后使用 setInterval 调用函数
setInterval("getTime",1000);