**PHP倒计时函数、求两个日期时间之间相差的时间函数、计算时差函数_PHP函数笔记**
```php
/**
* 求两个日期时间之间相差的时间
* (针对1970年1月1日之后,求之前可以采用泰勒公式)
* @param string $day1 2016-10-01 10:01:08
* @param string $day2 2020-10-01 10:01:08
* @return list
*/
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 国际」创作共享协议,可自由转载、引用,但需署名作者且注明文章出处
相关文章