简体   繁体   English

DateTime()计算两个日期之间的月数

[英]DateTime() Count Months Between Two Dates Issue

I'm using the below snippet of code which keeps returning 0 which isn't correct. 我正在使用下面的代码片段,该代码片段始终返回不正确的0。 $vtoday is the current date and the $pDate value is being checked against today to give a count of how many months have passed between the two dates. $vtoday是当前日期,今天将检查$pDate值,以计算两个日期之间经过了多少个月。

Now I've tried strtotime() on both dates before being passed into DateTime() with no luck, I get an non-numeric error or something along those lines. 现在,我在两个日期上都尝试过strtotime(),但是没有运气之前将其传递给DateTime(),我遇到了非数字错误或类似的错误。 I've also tried to format the two dates into 'Ymd' and then pass them into DateTime() with no luck either. 我还尝试将两个日期格式化为“ Ymd”,然后将它们传递到DateTime()中,也没有运气。

$vToday = date('Y-m-d H:i:s');
$pDate = '2013-03-11 12:13:03';

$vToday = new DateTime($vToday);
$vDate = new DateTime($pDate);
$vMonths = $vToday->diff($vDate)->m;

Am I going the completely wrong way to count the months between two dates or is there a simple problem with my logic? 我是用完全错误的方式来计算两个日期之间的月份吗,还是我的逻辑存在简单问题?

Thanks for any help, tips or suggestions. 感谢您的帮助,提示或建议。

UPDATE: 更新:

Figured it out, answer below. 想通了,在下面回答。

Value of m property of DateInterval class is always less than 12. If you want to get total number of months you have to add number of months multiplied by number of years: DateInterval类的m属性的值始终小于12。如果要获得总月数,则必须将月数乘以年数:

$vToday = date('Y-m-d H:i:s');
$pDate = '2013-03-11 12:13:03';

$vToday = new DateTime($vToday);
$vDate = new DateTime($pDate);
$vDiff = $vToday->diff($vDate);
$vMonths = $vDiff->m + 12* $vDiff->y;

Figured it out, the way DateTime() works is that if say there is 12 months it wont return 12 it will return 0 as 12 months is one year so if you use the ->y option you will get 1 and ->m will output as zero. 弄清楚了,DateTime()的工作方式是,如果说有12个月,它将不返回12,它将返回0,因为12个月是一年,因此,如果使用-> y选项,将得到1,而-> m将得到1。输出为零。

$vToday = date('Y-m-d H:i:s');
$pDate = '2013-03-11 12:13:03';

$vToday = new DateTime($vToday);
$vDate = new DateTime($pDate);
$vDiff = $vToday->diff($vDate);
var_dump($vDiff);

If I remove the ->m option and output the object ($vDiff) below is what you will see, this is why I was getting zero for the month value. 如果删除->m选项并输出下面的对象($ vDiff),这就是为什么我的月份值变为零的原因。

object(DateInterval)#13 (15) { 
  ["y"]=> int(1) 
  ["m"]=> int(0) 
  ["d"]=> int(23) 
  ["h"]=> int(16) 
  ["i"]=> int(6) 
  ["s"]=> int(43) 
  ["weekday"]=> int(0) 
  ["weekday_behavior"]=> int(0) 
  ["first_last_day_of"]=> int(0) 
  ["invert"]=> int(1) 
  ["days"]=> int(388)
  ["special_type"]=> int(0) 
  ["special_amount"]=> int(0) 
  ["have_weekday_relative"]=> int(0) 
  ["have_special_relative"]=> int(0) 
} 

you should use 你应该使用

$current_month = date('M');

It will return the current month. 它将返回当前月份。 and if you do date('Y') it will return the corresponding year. 如果您输入date('Y') ,它将返回相应的年份。

  1. Find the difference between two months 找出两个月之间的差异

  2. Check if the difference between the years is greater than 1, if so then add 12 to the month difference you found in step 1. 检查年份之间的差异是否大于1,如果是,则将12添加到在步骤1中找到的月份差异中。

Try this:- 尝试这个:-

$vToday = date('Y-m-d H:i:s');    
$pDate = '2013-03-11 12:13:03';    
$vToday = new DateTime($vToday);
$vDate = new DateTime($pDate);
$vDiff = $vToday->diff($vDate);
$vMonths = $vDiff->m + 12* $vDiff->y;
echo "Difference - ".$vMonths;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM