Rumah > pembangunan bahagian belakang > tutorial php > PHP怎样计算两个日期相差几个月?

PHP怎样计算两个日期相差几个月?

WBOY
Lepaskan: 2016-06-20 12:59:01
asal
911 orang telah melayarinya

<?phpfunction getMonthNum( $date1, $date2, $tags='-' ){  $date1 = explode($tags,$date1);  $date2 = explode($tags,$date2);  return abs($date1[0] - $date2[0]) * 12 + abs($date1[1] - $date2[1]); }echo getMonthNum("2013-02-01","2014-01-01",'-');?>
Salin selepas log masuk


以上代码计算出相差13个月,不知道该怎么改才能正常一些?


回复讨论(解决方案)

去掉 abs
返回 -11 表示 2013-02-01 比 2014-01-01 少 11 个月
当然你也可以将整个返回值 abs,但这样就只是知道差 11 个月,但不知道是多了还是少了

function getMonthNum($date1,$date2){	$date1_stamp=strtotime($date1);	$date2_stamp=strtotime($date2);	list($date_1['y'],$date_1['m'])=explode("-",date('Y-m',$date1_stamp));	list($date_2['y'],$date_2['m'])=explode("-",date('Y-m',$date2_stamp));	return abs($date_1['y']-$date_2['y'])*12 +$date_2['m']-$date_1['m']; } echo getMonthNum("2013-02-01","2014-01-01");echo getMonthNum("20130201","20140101");echo getMonthNum("201302","201401");
Salin selepas log masuk
Salin selepas log masuk

<?phpfunction getMonthNum( $date1, $date2, $tags='-' ){  $date1 = explode($tags,$date1);  $date2 = explode($tags,$date2);  if ($date1[0]>=$date2[0]){  return abs($date1[0] - $date2[0]) * 12 + abs($date1[1] - $date2[1]);  }  else{  return abs($date2[0] - $date1[0]) * 12 - abs($date1[1] - $date2[1]);  } }echo getMonthNum("2013-02-01","2014-01-01",'-');?>
Salin selepas log masuk

上面写错了。。。。。重发:

function getMonthNum($date1,$date2){	$date1_stamp=strtotime($date1);	$date2_stamp=strtotime($date2);	list($date_1['y'],$date_1['m'])=explode("-",date('Y-m',$date1_stamp));	list($date_2['y'],$date_2['m'])=explode("-",date('Y-m',$date2_stamp));	return abs(($date_2['y']-$date_1['y'])*12 +$date_2['m']-$date_1['m']); } echo getMonthNum("2013-02-01","2014-01-01");echo getMonthNum("20130201","20140101");echo getMonthNum("201302","201401");
Salin selepas log masuk

function getMonthNum($date1,$date2){	$date1_stamp=strtotime($date1);	$date2_stamp=strtotime($date2);	list($date_1['y'],$date_1['m'])=explode("-",date('Y-m',$date1_stamp));	list($date_2['y'],$date_2['m'])=explode("-",date('Y-m',$date2_stamp));	return abs($date_1['y']-$date_2['y'])*12 +$date_2['m']-$date_1['m']; } echo getMonthNum("2013-02-01","2014-01-01");echo getMonthNum("20130201","20140101");echo getMonthNum("201302","201401");
Salin selepas log masuk
Salin selepas log masuk

echo getMonthNum("2013-02-01","2012-01-01");//11
这对吗?

$start_time = "2014-01-01";
$end_time = "2015-05-08";
for (;1==1;){
echo date("Y-m",strtotime($start_time))."
";
$end_time = date("Y-m",strtotime( $end_time ));

if($start_time==$end_time){
break;
}
$start_time = date("Y-m",strtotime("$start_time +1 month"));
}

function getMonthNum( $date1, $date2, $tags='-'){  $time1 = strtotime($date1);  $time2 = strtotime($date2);  $date1 = explode($tags,$date1);  $date2 = explode($tags,$date2);  $months =abs($date1[0]-$date2[0])*12;  if($time1 > $time2){	return $months+$date1[1]-$date2[1];  }else{	return -($months+$date2[1]-$date1[1]);  } }
Salin selepas log masuk

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan