php – 2个时间()值之间的差异
作者:互联网
我有2个time()值,例如:
1379078542
1379078574
我如何解决他们的不确定性并将输出显示为年,月,周,日,小时,分钟,秒等.
我认为上面的2个值有32秒的差异,所以在这种情况下,我想显示32秒.
解决方法:
$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;
标签:php,timestamp,datetime,date-math 来源: https://codeday.me/bug/20190930/1834313.html