2023-11-10 05:20:33
1、首先计算2020-02-10和2020-02-01日间隔的天数。使用strtotime:<?php $days = (strtotime('2020-02-10') - strtotime('2020-02-01'))/86400; echo $days;。

2、运行之后如下图,显示间隔天数。

3、还可以使用new DateTime + diff:$date1 = new DateTime('2020-02-01');$date2 = new DateTime('2020-02-10');$cha = $date1->diff($date2);echo $cha->format('%R%a days');。

4、使用date_create + date_diff。

5、都可以计算出间隔天数。

2023-01-20 06:00:23
2021-01-26 13:05:17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
/* *function:计算两个日期相隔多少年,多少月,多少天 *param string $date1[格式如:2011-11-5] *param string $date2[格式如:2012-12-01] *return array array('年','月','日'); */ function diffDate($date1,$date2){ if(strtotime($date1)>strtotime($date2)){ $tmp=$date2; $date2=$date1; $date1=$tmp; } list($Y1,$m1,$d1)=explode('-',$date1); list($Y2,$m2,$d2)=explode('-',$date2); $Y=$Y2-$Y1; $m=$m2-$m1; $d=$d2-$d1; if($d<0){ $d+=(int)date('t',strtotime("-1 month $date2")); $m--; } if($m<0){ $m+=12; $y--; } return array('year'=>$Y,'month'=>$m,'day'=>$d); } ///调用方法:echo '<pre>';print_r(diffDate('2014-12-03','2000-12-01'));//结果:/*Array( [year] => 14 [month] => 0 [day] => 2)*/