計算時間相差年、月、天、時、分、秒
阿新 • • 發佈:2019-01-31
<?php
/**
* function:計算兩個日期相隔多少年,多少月,多少天
* param string $date1[格式如:2011-11-5]
* param string $date2[格式如:2012-12-01]
* return array array('年','月','日');
*/
function diffDate($date1,$date2)
{
$datetime1 = new \DateTime($date1);
$datetime2 = new \DateTime($date2);
$interval = $datetime1->diff($datetime2);
$time['y'] = $interval->format('%Y');
$time['m'] = $interval->format('%m');
$time['d'] = $interval->format('%d');
$time['h'] = $interval->format('%H');
$time['i'] = $interval->format('%i');
$time['s'] = $interval->format('%s');
$time['a'] = $interval->format('%a'); // 兩個時間相差總天數
return $time;
}
# 使用例項
$sss = diffDate('2015-12-25 12:30:30', '2015-12-26 15:00:00');
print_r($sss);
# 輸出
Array
(
[y] => 00
[m] => 0
[d] => 1
[h] => 02
[i] => 29
[s] => 30
[a] => 1
)
/**
* function:計算兩個日期相隔多少年,多少月,多少天
* param string $date1[格式如:2011-11-5]
* param string $date2[格式如:2012-12-01]
* return array array('年','月','日');
*/
function diffDate($date1,$date2)
{
$datetime1 = new \DateTime($date1);
$datetime2 = new \DateTime($date2);
$interval = $datetime1->diff($datetime2);
$time['y'] = $interval->format('%Y');
$time['m'] = $interval->format('%m');
$time['d'] = $interval->format('%d');
$time['h'] = $interval->format('%H');
$time['i'] = $interval->format('%i');
$time['s'] = $interval->format('%s');
$time['a'] = $interval->format('%a'); // 兩個時間相差總天數
return $time;
}
# 使用例項
$sss = diffDate('2015-12-25 12:30:30', '2015-12-26 15:00:00');
print_r($sss);
# 輸出
Array
(
[y] => 00
[m] => 0
[d] => 1
[h] => 02
[i] => 29
[s] => 30
[a] => 1
)