Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

Financial year select menu

David Carr

Tutorials PHP & MySQL

Table of Contents

Financial years start from April to March, a common requirement is to be able to select a financial year from a select menu.

First by creating an array of years.

$dates = range('2016', date('Y'));

Loop over the years

foreach($dates as $date){

    if (date('m', strtotime($date)) <= 6) {//Upto June
        $year = ($date-1) . '-' . $date;
    } else {//After June
        $year = $date . '-' . ($date + 1);
    }

    echo "<option value='$year'>$year</option>";
}

if the month is less than or equal to June then set the year minus 1 year and add the current year (2016 - 2017) otherwise select the year and add the next year (2017 - 2018)

Putting it all together

<label class="control-label" for='year'> Year</label>
<select name="year" class="form-control">
<option value="">Select</option>
<?php
$dates = range('2016', date('Y'));
foreach($dates as $date){

    if (date('m', strtotime($date)) <= 6) {//Upto June
        $year = ($date-1) . '-' . $date;
    } else {//After June
        $year = $date . '-' . ($date + 1);
    }

    echo "<option value='$year'>$year</option>";
}
?>
</select>

 

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.