-2

I want to calculate the number of months between two dates.

For example:

2022-10-19 - 2022-12-06 ~ 1,6 months

I tried this, but not every month is 30 days, so it is not a good solution:

$start = new DateTimeImmutable("2022-10-19");
$end = new DateTimeImmutable("2022-12-06");
$diffbetween = $start->diff($end);
$diffbetweenresult = round(($diffbetween->format('%a')) / 30, 2);

Can you help me?

Thank you very much!

  • What are you trying to achieve (eg. what is this value used for)? As you say, not all months have the same length so the number you'll get will not be relevant/consistant: 1.6 months from start of February to March will not be equal to 1.6 months from start of March to April so these values will not be comparable – Kaddath Dec 06 '22 at 10:09
  • Does this answer your question? [PHP Carbon diffInMonths](https://stackoverflow.com/questions/43880879/php-carbon-diffinmonths) – jspit Dec 06 '22 at 10:38
  • I need exact values because I have to count on returns on investments. – Péter Gergő Dec 06 '22 at 12:06
  • Then I think that you should either use the number of days to do your calculations, or isolate moths separately. You can take the exemple #3 from [the manual](https://www.php.net/manual/en/dateinterval.format.php) to easily get the remaining days. As I said in my first comment, using this kind of float value will lead you to results that doesn't add up, as the same value can mean different interval length – Kaddath Dec 06 '22 at 13:40
  • I think Carbon's FloatDiffInMonths method is exactly what Péter Gergő is looking for. The documentation https://carbon.nesbot.com/docs/ also contains comments on how the value is calculated. – jspit Dec 06 '22 at 14:55
  • @jspit you're right, I hadn't read with enough care – Kaddath Dec 06 '22 at 16:38
  • The months are not the same length. This has already been pointed out. The question now is what weight the individual days are given. Should each day have the same difficulty or are 14 days in February (= 1/2 month) just as difficult as 15 days in April (also 1/2 month)? – jspit Dec 06 '22 at 19:19
  • There is a daily interest settlement, but my full php code able to calculate only in month, that is why I need to know the exact value. For example: there is a 15% yield for 365 days. Between 2022-10-19 - 2022-12-06, there is ~ 1,6 months, so 15/365*[exact days in the 1,6 month]. – Péter Gergő Dec 07 '22 at 06:39

1 Answers1

1

If all days are to be weighted equally for the calculation, you can only proceed as follows. The year has an average of 365.25 days (every 4 years 366 days), a month then has an average of 30.4375 days or 2629800 seconds. The calculation for this can be done in one line:

$start = "2022-10-19";
$end = "2022-12-06";
$floatMonth = (strtotime($end)-strtotime($start))/2629800;
//$floatMonth: float(1.5783709787817)

This calculation does not deliver an integer result even with differences from integer months. The result for the half year from "2022-07-01" - "2023-01-01" is 6.0465. The result does not correspond to the general expectation. Only the period of 4 years results in 48 months.

jspit
  • 7,276
  • 1
  • 9
  • 17