История изменений
Исправление Noob_Linux, (текущая версия) :
Предложу свой велосипед
<?php
function birthdayCount(DateTimeImmutable $birthDay, DateTimeImmutable $from, DateTimeImmutable $to): int
{
if($to < $birthDay) {
return 0;
}
$firstBirthDay = new DateTimeImmutable("{$birthDay->format('d-m')}-{$from->format('Y')}");
$period = new DatePeriod(
$firstBirthDay,
new DateInterval('P1Y'),
$to
);
return count(iterator_to_array($period->getIterator())) - 1;
}
// tests
echo birthdayCount(
new DateTimeImmutable('23-03-2000'),
new DateTimeImmutable('01-01-2000'),
new DateTimeImmutable('31-12-2020'),
) === 20, PHP_EOL;
echo birthdayCount(
new DateTimeImmutable('23-03-2000'),
new DateTimeImmutable('01-01-2005'),
new DateTimeImmutable('31-12-2020'),
) === 15, PHP_EOL;
echo birthdayCount(
new DateTimeImmutable('23-03-2000'),
new DateTimeImmutable('01-01-2000'),
new DateTimeImmutable('31-12-2015'),
) === 15, PHP_EOL;
Исходная версия Noob_Linux, :
Предложу свой велосипед
<?php
function birthdayCount(DateTimeImmutable $birthDay, DateTimeImmutable $from, DateTimeImmutable $to): int
{
$firstBirthDay = new DateTimeImmutable("{$birthDay->format('d-m')}-{$from->format('Y')}");
$period = new DatePeriod(
$firstBirthDay,
new DateInterval('P1Y'),
$to
);
return count(iterator_to_array($period->getIterator())) - 1;
}
// tests
echo birthdayCount(
new DateTimeImmutable('23-03-2000'),
new DateTimeImmutable('01-01-2000'),
new DateTimeImmutable('31-12-2020'),
) === 20, PHP_EOL;
echo birthdayCount(
new DateTimeImmutable('23-03-2000'),
new DateTimeImmutable('01-01-2005'),
new DateTimeImmutable('31-12-2020'),
) === 15, PHP_EOL;
echo birthdayCount(
new DateTimeImmutable('23-03-2000'),
new DateTimeImmutable('01-01-2000'),
new DateTimeImmutable('31-12-2015'),
) === 15, PHP_EOL;