0

该事件返回格式为“Sat Oct 25 21:55:29 +0000 2008”的标准化时间戳我如何将其与 PHP 中的当前时间进行比较,以便它以类似“已经 15 岁”的形式给出结果分钟!” 或“已经三天了!” 另外,我怎样才能得到当前时间?

4

1 回答 1

6

首先使用strtotime()将该字符串转换为 UNIX 时间戳:

$event_time = strtotime('Sat Oct 25 21:55:29 +0000 2008');

然后使用time()获取当前的 UNIX 时间戳:

$current_time = time();

然后获取这些时间戳之间的秒数:

$difference = $current_time - $event_time;

最后使用除法将秒数转换为天数或小时数或其他:

$hours_difference = $difference / 60 / 60;
$days_difference = $difference / 60 / 60 / 24;

有关计算相对时间的更多信息,请参阅如何计算相对时间?.

于 2008-11-01T06:42:51.440 回答