php日期相减函数,倒计时函数_计算两个时间相差值_PHP函数
**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 國際」創作共享協議,可自由轉載、引用,但需署名作者且注明文章出處
相關文章
總結
以上是生活随笔為你收集整理的php日期相减函数,倒计时函数_计算两个时间相差值_PHP函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大学生信用卡透支额度 可千万不要透支信用
- 下一篇: php fast cgi nginx,通