2个time()值之间的差异

时间:2013-09-13 13:38:47

标签: php datetime timestamp date-math

我有2个time()值,例如:

1379078542
1379078574

如何解决他们的不确定性并将输出显示为年,月,周,日,小时,分钟,秒等。

我认为上面的2个值有32秒的差异,所以在这种情况下,我想显示32秒。

1 个答案:

答案 0 :(得分:6)

$datetime1 = new DateTime('@1379078542');
$datetime2 = new DateTime('@1379078574');
$interval  = $datetime1->diff($datetime2);
$elapsed   = $interval->format('%y years, %m months, %a days, %h hours, %i minutes, %S seconds');
$elapsed   = str_replace(array('0 years,', ' 0 months,', ' 0 days,',  ' 0 hours,', ' 0 minutes,'), '', $elapsed);
$elapsed   = str_replace(array('1 years, ', ' 1 months, ', ' 1 days, ',  ' 1 hours, ', ' 1 minutes'), array('1 year, ', '1 month, ', ' 1 day, ', ' 1 hour, ', ' 1 minute'), $elapsed);
echo $elapsed;

<强> See it in action