Answer the question
In order to leave comments, you need to log in
How to count the number of affected months between two dates?
$date1 = strtotime('08.10.2019');
$date2 = strtotime('10.11.2019');
Answer the question
In order to leave comments, you need to log in
I also looked for options, a lot of frightening)
I got it like this:
SELECT COUNT(
DISTINCT CONCAT(
MONTH(date),
YEAR(date)
)
)
FROM `table_example` WHERE date IS NOT NULL
They show everything correctly, there will always be 1 month, the difference between them is clear.
Do +1, xs.
Although here
$start = (new DateTime('08-10-2019'))->modify('first day of this month');
$end = (new DateTime('10-11-2019'))->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
$countMonth = 0;
foreach ($period as $dt) {
echo $dt->format("Y-m") . "<br>\n";
++$countMonth;
}
echo $countMonth;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question