循环计时器与PHP

时间:2013-04-14 16:44:12

标签: php

我有WEBVTT个文件连接视频,视频长度约为120分钟。视频的缩略图顶片每秒都在运行,这意味着120*60=7200 secs.

如何使用php循环功能将7200秒转换为WEBVTT format(hh:mm:ss.ttt)?例如:

00:00:00.000 --> 00:00:01.000
00:00:01.000 --> 00:00:02.000
00:00:02.000 --> 00:00:03.000
00:00:03.000 --> 00:00:04.000    
and so on...    

谢谢!

2 个答案:

答案 0 :(得分:1)

使用date()

date_default_timezone_set('UTC'); // To fix some timezone problems

$start = 0; // 0h
$end = 7200; // 2h
$output = '';

for($i=$start;$i<$end;$i++){
    $output .= date('H:i:s', $i).'.000 --> '.date('H:i:s', $i+1).'.000'.PHP_EOL;
}

echo $output;

请注意,如果$ limit达到86400,它将再次从0开始。

答案 1 :(得分:1)

我不认为PHP是正确的工具。听起来像Javascript可能就是你想要在屏幕上为用户显示的内容。

对于PHP,您可以使用日期函数

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);