Skip to content
Advertisement

Get start and end date of the financial year in Carbon

I need to dynamically echo the start and end dates of the current Australian financial year (July 1st to June 30 of the year).

I am using Carbon and Laravel to get the dates of the current financial year.

I assume the code will look something like this semi-pseudocode

$now = now();
if( $now > $july_first_current_year ) {
    $year_start = Carbon::createFromFormat('Y-m-d H:s:i', $now->subYear()->year . '-7-1 0:00:01');
    $year_end = Carbon::createFromFormat('Y-m-d H:s:i', $now->year . '-6-30 0:00:01');
} else {
    $year_start = Carbon::createFromFormat('Y-m-d H:s:i', $now->year . '-7-1 0:00:01');
    $year_end = Carbon::createFromFormat('Y-m-d H:s:i', $now->addYear()->year . '-6-30 0:00:01');
}

As you can see my math is not quite right and it seems im missing a step or two to output the right dates. Or perhaps im missing something simple?

Advertisement

Answer

Try this :

 $start_year = CarbonCarbon::createFromFormat('Y-m-d H:s:i', date('Y').'-07-01 00:00:00');
 $end_year = CarbonCarbon::createFromFormat('Y-m-d H:s:i', date('Y').'-06-30 23:59:59.999999');

 if(now() > $start_year)        
     $start_year = $start_year->subYear(1);        
 else
     $end_year = $end_year->addYear(1);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement