**PHP倒計時函數、求兩個日期時間之間相差的時間函數、計算時差函數_PHP函數筆記**
```php
function diffBetweenTwoDate($start_time, $end_time){
$start = strtotime($start_time);
$stop = strtotime($end_time);
if($start < $stop){
$total_days = ($stop-$start) / 86400;
$result['total_days'] = intval($total_days);
$result['year'] = 0;
$result['month'] = 0;
if($total_days < 7){//如果小于7天直接傳回天數
$result['day'] = intval($total_days);
}elseif($total_days <= 31){//小于28天則傳回周數,由于閏年2月滿足了
if($stop == strtotime($start_time.'+1 month')){
$result['month'] = 1;
}else{
//$w = floor($total_days / 7);
//$d = ($stop - strtotime($start_time.'+'.$w.' week')) / 86400;
//$result['week'] = $w;
//$result['day'] = intval($d);
$result['day'] = intval($total_days);
}
}else{
$y= floor($total_days / 365);
if($y >= 1){//如果超過一年
$start = strtotime($start_time.'+'.$y.'year');
$start_time = date('Y-m-d',$start);
//判斷是否真的已經有了一年了,如果沒有的話就開減
if($start>$stop){
$start_time = date('Y-m-d',strtotime($start_time.'-1 month'));
$m = 11;
$y--;
}
$total_days = ($stop - strtotime($start_time)) / 86400;
}
if(isset($m)){
$w = floor($total_days / 7);
$d = $total_days-$w * 7;
}else{
$m = isset($m) ? $m : round($total_days / 30);
$stop >= strtotime($start_time.'+'.$m.'month') ? $m : $m--;
if($stop >= strtotime($start_time.'+'.$m.'month')){
$d = $w = ($stop-strtotime($start_time.'+'.$m.'month')) / 86400;
//$w = floor($w / 7);
//$d = $d - $w * 7;
}
}
$result['year'] = $y;
$result['month'] = $m;
//$result['week'] = $w;
$result['day'] = intval(isset($d) ? $d : 0);
}
$result['hours'] = intval((($stop - $start) % 86400) / 3600);
$result['min'] = intval(((($stop - $start) % 86400) % 3600) / 60);
$result['sec'] = ((($stop - $start) % 86400) % 3600) % 60;
}else{
$result['total_days'] = 0;
$result['year'] = 0;
$result['month'] = 0;
$result['day'] = 0;
$result['hours'] = 0;
$result['min'] = 0;
$result['sec'] = 0;
}
//return array_filter($result);
return $result;
}
```
傳回字段**年、月、日、時、分、秒**
最後更新于 2019-07-20 16:39:20 并被添加「倒計時 php函數筆記」标簽,已有 14971 位童鞋閱讀過。
本站使用「署名 4.0 國際」創作共享協定,可自由轉載、引用,但需署名作者且注明文章出處
相關文章