Home:ALL Converter>Calculate months diff between dates without days

Calculate months diff between dates without days

Ask Time:2018-12-07T01:21:57         Author:johnyT

Json Formatter

is there a good way to calculate months diff between dates without days? I mean I have two not full dates for example: 2017-09 and 2018-11. I need to calculate how many months is between this two dates. I read something about this and I know I can for example use:

$firstDate = "2017-09";
$secondDate = "2018-11";
$firstDate = new DateTime($firstDate . "-01"); 
$secondDate  = new DateTime($secondDate . "-01");
$interval = date_diff($firstDate, $secondDate);

var_dump($interval->format('%m months'));exit();

This show me 2 months. How can I reach this? And is there a way to calculate this without adding days "-01" to end of my dates? I want to calculate difference of months for dates without write "-01" in this dates. Only year and month.

Author:johnyT,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53656663/calculate-months-diff-between-dates-without-days
Metabolic :

I will suggest using Carbon for dates related calculations in PHP as it really makes everything easy. \n\nTo calculate months between two dates with Carbon, you simply need to do this\n\n//year & month\n$startDate = Carbon::create('2017', '9');\n$endDate = Carbon::create('2018','11');\n$diff = $startDate->diffInMonths($endDate);\n",
2018-12-06T17:35:27
Andreas :

You need the years also.\nAlso you had a parse error on the date 2017-0901 is not valid either 2017-09 or 2017-09-01.\n\n$firstDate = \"2017-09\";\n$secondDate = \"2018-11\";\n$firstDate = new DateTime($firstDate); \n$secondDate = new DateTime($secondDate);\n$interval = date_diff($firstDate, $secondDate);\n\necho $interval->format('%y')*12+$interval->format('%m') . \" months\";\n// 14 months\n\n\nhttps://3v4l.org/XGdXg",
2018-12-06T17:28:35
Mike 'Pomax' Kamermans :

Just use plain arithmetic. We're clearly not concerned about timezones, daylight saving, calendar changes, etc. so we're also not concerned about parsing the date \"in some timezone, based on some calendar\". What we're left with is just plain arithemtics, using months. A year is 12 months. And now we're almost done already.\n\nfunction ym_as_months($v) {\n $v = array_map(\"intval\", explode(\"-\", $v));\n return $v[0]*12 + $v[1];\n}\n\n$firstDate = \"2017-09\"; \n$firstMonths = ym_as_months($firstDate);\n\n$secondDate = \"2018-11\"; \n$secondMonths = ym_as_months($secondDate);\n\n$diff = $secondMonths - $firstMonths;\necho \"There are $diff months between $firstDate and $secondDate.\";\n\n\nAnd we get:\n\n\n There are 14 months between 2017-09 and 2018-11.\n\n\nPerfect.\n\nOf course, depending on how you get those date stamps in your application, it might be far easier to not even pass them in as string, but simply as two numbers from the get go, in which case this becomes even less work.\n\nAlternatively, do your conversion as the very last step, as per another answer here.",
2018-12-06T19:00:47
yy