0

我正在解析字幕文件(srt 格式),下面是一行对话的示例:

27
00:01:32,400 --> 00:01:34,300
Maybe they came back
for Chinese food.

时间以格式出现

hours:minutes:seconds,milliseconds

我想操纵这些时间并进行比较,但我遇到的各种 PHP 类似乎不支持毫秒。


我的问题:

我想做的一件事是解析2个用于同一媒体的字幕文件(例如,同一部电影或同一电视剧集等),并比较每个字幕文件的文本以获得相同的对话行。问题是同一行的开始和结束时间会稍微偏离几百毫秒。例如,以上面的行为例,在另一个字幕文件中,同一行的时间是

00:01:32,320 --> 00:01:34,160

要获得同一对话行的两个文件的版本,您可以检查文件二中是否有一行位于文件一个开始和结束时间的几百毫秒内,并且应该捕获它。类似的东西。所以我需要通过向它们添加毫秒来操纵时间,并比较这些时间。

4

2 回答 2

2

假设您使用的是 PHP >=5.3(对于 是必需的getTimestamp()),这将起作用:

$unformatted_start = '00:01:32,400';
$unformatted_end = '00:01:34,300';

// Split into hh:mm:ss and milliseconds
$start_array = explode(',', $unformatted_start);
$end_array = explode(',', $unformatted_end);

// Convert hh:mm:ss to DateTime
$start  = new DateTime($start_array[0]);
$end = new DateTime($end_array[0]);

// Convert to time in seconds (PHP >=5.3 only)
$start_in_seconds = $start->getTimestamp();
$end_in_seconds = $end->getTimestamp();

// Convert to milliseconds, then add remaining milliseconds
$start_in_milliseconds = ($start_in_seconds * 1000) + $start_array[1];
$end_in_milliseconds = ($end_in_seconds * 1000) + $end_array[1];

// Calculate absolute value of the difference between start and end
$elapsed = abs($start_in_milliseconds - $end_in_milliseconds);

echo $elapsed; // 1900
于 2013-06-13T14:50:18.030 回答
0

你试过strtotime吗?

if (strtotime($date1) > strtotime($date2)) { # date1 is after date2
    # do work here
} 
if (strtotime($date1) < strtotime($date2)) { #date2 is after date1
    # do other work here
}
于 2013-06-13T14:26:25.903 回答