我怎样才能找到事件发生后的时间?

chu*_*tar 0 php time datetime

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

Jer*_*ten 6

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

$event_time = strtotime('Sat Oct 25 21:55:29 +0000 2008');
Run Code Online (Sandbox Code Playgroud)

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

$current_time = time();
Run Code Online (Sandbox Code Playgroud)

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

$difference = $current_time - $event_time;
Run Code Online (Sandbox Code Playgroud)

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

$hours_difference = $difference / 60 / 60;
$days_difference = $difference / 60 / 60 / 24;
Run Code Online (Sandbox Code Playgroud)

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