-1

I am needing assistance with the following. In Australia a financial year runs from 1 July 20XX to 30 June 20YY. I need code to return either 365 or 366 total days depending upon whether it is a leap year. For example, if the input date is 16 August 2023, then the financial year of 1 July 2023 to 30 June 2024 contains the additional day as it is a leap year so the returned result is 366. This is because February having 29 days falls within that date range.

Please note that the software we are using has the following restriction:

*Note: You cannot use objects (OOP) in calculated fields. Instead you should use the php function like date() or strtotime() etc

Thank you very much for your assistance.

1 Answers1

0

my solution would be this:

<?php

$date = new DateTime('2023-04-01');

if ($date->format('m') <= 6) {
    // get the lower fin year
    $startYear = $date->format('Y') - 1;
    $endYear = $date->format('Y');
} else {
    // get the next year
    $startYear = $date->format('Y');
    $endYear = $date->format('Y') + 1;
}

$startFinDate = new DateTime($startYear . '-07-01');
$endFinDate = new DateTime($endYear . '-07-01');

$finInterval = $endFinDate->diff($startFinDate);

echo $date->format('d.m.Y') . ' -> "' . $startFinDate->format('d.m.Y') . " - " . $endFinDate->format('d.m.Y') . '" -> ' . $finInterval->days . PHP_EOL;

output for 2023-04-01: 01.04.2023 -> "01.07.2022 - 01.07.2023" -> 365

output for 2023-08-16: 16.08.2023 -> "01.07.2023 - 01.07.2024" -> 366


if you are not allowed to use OOP, you need to calculate manually, if the lower year (months are less then "july") is a leap year. there are dozens of algorithms for that in the net.

rickroyce
  • 950
  • 11
  • 24