php 计算PHP中两个日期之间的月数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13416894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate the number of months between two dates in PHP?
提问by cronoklee
Without using PHP 5.3's date_difffunction (I'm using PHP 5.2.17), is there a simple and accurate way to do this? I am thinking of something like the code below, but I don't know how to account for leap years:
不使用 PHP 5.3 的date_diff函数(我使用的是 PHP 5.2.17),有没有一种简单而准确的方法来做到这一点?我正在考虑类似下面的代码,但我不知道如何计算闰年:
$days = ceil(abs( strtotime('2000-01-25') - strtotime('2010-02-20') ) / 86400);
$months = ???;
I'm trying to work out the number of months old a person is.
我正在尝试计算一个人的月数。
回答by deceze
$date1 = '2000-01-25';
$date2 = '2010-02-20';
$ts1 = strtotime($date1);
$ts2 = strtotime($date2);
$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);
$month1 = date('m', $ts1);
$month2 = date('m', $ts2);
$diff = (($year2 - $year1) * 12) + ($month2 - $month1);
You may want to include the days somewhere too, depending on whether you mean wholemonths or not. Hope you get the idea though.
您可能还想在某处包含这些天,具体取决于您是否指的是整月。但希望你能明白。
回答by pollux1er
This is a simple method I wrote in my class to count the number of months involved into two given dates :
这是我在课堂上编写的一个简单方法,用于计算两个给定日期所涉及的月数:
public function nb_mois($date1, $date2)
{
$begin = new DateTime( $date1 );
$end = new DateTime( $date2 );
$end = $end->modify( '+1 month' );
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($begin, $interval, $end);
$counter = 0;
foreach($period as $dt) {
$counter++;
}
return $counter;
}
回答by NIKHIL PARIHAR
Here is my solution. It checks both years and months of dates and find difference.
这是我的解决方案。它检查日期的年份和月份并找出差异。
$date1 = '2000-01-25';
$date2 = '2010-02-20';
$d1=new DateTime($date2);
$d2=new DateTime($date1);
$Months = $d2->diff($d1);
$howeverManyMonths = (($Months->y) * 12) + ($Months->m);
回答by Adam
Like this:
像这样:
$date1 = strtotime('2000-01-25');
$date2 = strtotime('2010-02-20');
$months = 0;
while (($date1 = strtotime('+1 MONTH', $date1)) <= $date2)
$months++;
echo $months;
If you want to include days to, then use this:
如果要包括天数,请使用以下命令:
$date1 = strtotime('2000-01-25');
$date2 = strtotime('2010-02-20');
$months = 0;
while (strtotime('+1 MONTH', $date1) < $date2) {
$months++;
$date1 = strtotime('+1 MONTH', $date1);
}
echo $months, ' month, ', ($date2 - $date1) / (60*60*24), ' days'; // 120 month, 26 days
回答by gattsbr
I recently needed to calculate age in months ranging from prenatal to 5 years old (60+ months).
我最近需要计算从产前到 5 岁(60 个月以上)的月龄。
Neither of the answers above worked for me. The first one I tried, which is basically a 1 liner for deceze's answer
上面的答案都不适合我。我试过的第一个,基本上是 deceze 回答的 1 个班轮
$bdate = strtotime('2011-11-04');
$edate = strtotime('2011-12-03');
$age = ((date('Y',$edate) - date('Y',$bdate)) * 12) + (date('m',$edate) - date('m',$bdate));
. . .
This fails with the set dates, obviously the answer should be 0 as the month mark (2011-12-04) hasn't been reached yet, how ever the code returns 1.
这在设置日期时失败,显然答案应该是 0,因为尚未达到月份标记 (2011-12-04),代码如何返回 1。
The second method I tried, using Adam's code
我尝试的第二种方法,使用亚当的代码
$bdate = strtotime('2011-01-03');
$edate = strtotime('2011-02-03');
$age = 0;
while (strtotime('+1 MONTH', $bdate) < $edate) {
$age++;
$bdate = strtotime('+1 MONTH', $bdate);
}
. . .
This fails and says 0 months, when it should be 1.
这失败并说 0 个月,而它应该是 1。
What did work for me, is a little expansion of this code. What I used is the following:
对我有用的是对这段代码的一点扩展。我使用的是以下内容:
$bdate = strtotime('2011-11-04');
$edate = strtotime('2012-01-04');
$age = 0;
if($edate < $bdate) {
//prenatal
$age = -1;
} else {
//born, count months.
while($bdate < $edate) {
$age++;
$bdate = strtotime('+1 MONTH', $bdate);
if ($bdate > $edate) {
$age--;
}
}
}
回答by Logan Wayne
Follow up on the answer of @deceze (I've upvoted on his answer). Month will still count as a whole even if the dayof the first date didn't reached the day of the second date.
跟进@deceze 的回答(我对他的回答投了赞成票)。本月将仍然算作一个整体,即使一天的第一次约会都没有达到第二次约会的日子。
Here's my simple solution on including the day:
这是我关于包括这一天的简单解决方案:
$ts1=strtotime($date1);
$ts2=strtotime($date2);
$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);
$month1 = date('m', $ts1);
$month2 = date('m', $ts2);
$day1 = date('d', $ts1); /* I'VE ADDED THE DAY VARIABLE OF DATE1 AND DATE2 */
$day2 = date('d', $ts2);
$diff = (($year2 - $year1) * 12) + ($month2 - $month1);
/* IF THE DAY2 IS LESS THAN DAY1, IT WILL LESSEN THE $diff VALUE BY ONE */
if($day2<$day1){ $diff=$diff-1; }
The logic is, if the day of the second date is less than the day of the first date, it will reduce the value of $diffvariable by one.
逻辑是,如果第二个日期的天数小于第一个日期的天数,则将$diff变量的值减一。
回答by manix
How about this:
这个怎么样:
$d1 = new DateTime("2009-09-01");
$d2 = new DateTime("2010-09-01");
$months = 0;
$d1->add(new \DateInterval('P1M'));
while ($d1 <= $d2){
$months ++;
$d1->add(new \DateInterval('P1M'));
}
print_r($months);
回答by Khuat Huu Vinh
my function to resolve issue
我解决问题的功能
function diffMonth($from, $to) {
$fromYear = date("Y", strtotime($from));
$fromMonth = date("m", strtotime($from));
$toYear = date("Y", strtotime($to));
$toMonth = date("m", strtotime($to));
if ($fromYear == $toYear) {
return ($toMonth-$fromMonth)+1;
} else {
return (12-$fromMonth)+1+$toMonth;
}
}
回答by Evgeny
Here is my solution. It's only checks years and monthes of dates. So, if one date is '31.10.15' and other is '02.11.15' it returns 1 month.
这是我的解决方案。它只检查日期的年份和月份。因此,如果一个日期是“31.10.15”而另一个日期是“02.11.15”,则返回 1 个月。
function get_interval_in_month($from, $to) {
$month_in_year = 12;
$date_from = getdate(strtotime($from));
$date_to = getdate(strtotime($to));
return ($date_to['year'] - $date_from['year']) * $month_in_year -
($month_in_year - $date_to['mon']) +
($month_in_year - $date_from['mon']);
}
回答by Bhuvan Arora
$date1 = '2000-01-25';
$date2 = '2010-02-20';
$ts1 = strtotime($date1);
$ts2 = strtotime($date2);
$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);
$month1 = date('m', $ts1);
$month2 = date('m', $ts2);
$diff = (($year2 - $year1) * 12) + ($month2 - $month1);
If Month switched from Jan to Feb above code will return you the $diff = 1 But if you want to consider next month only after 30 days then add below lines of code along with above.
如果月份从一月切换到二月,上面的代码将返回 $diff = 1 但如果你只想在 30 天后考虑下个月,那么在上面添加下面的代码行。
$day1 = date('d', $ts1);
$day2 = date('d', $ts2);
if($day2 < $day1){ $diff = $diff - 1; }

