Home >Backend Development >PHP Tutorial > 请教php中两个时间怎么相减

请教php中两个时间怎么相减

WBOY
WBOYOriginal
2016-06-13 13:01:341275browse

请问php中两个时间如何相减?
有一个已知时间格式为2012-11-22 12:05:07 我想用现在时间格式为:2012-11-23 16:13:08减去第一个时间,即用2012-11-23 16:13:08减去2012-11-22 12:05:07得出已用时间1天4小时8分1秒。
------解决方案--------------------
$time = time()-strtotime('2012-11-22 12:00:00');
$yourday = (int)($time/(3600*24));
$yourhour = (int)(($time%(3600*24))/(3600));
$yourmin = (int)($time%(3600)/60);
echo $yourday.'天'.$yourhour.'小时'.$yourmin.'分';
//1天4小时34分
------解决方案--------------------

$datetime1 = new DateTime('2012-11-23 16:13:08');<br />
$datetime2 = new DateTime('2012-11-22 12:05:07');<br />
$interval = $datetime1->diff($datetime2);<br />
echo $interval->format('%d天%H小时%i分%s秒');<br />

1天04小时8分1秒

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn