Tutorials, PHP & MySQL

Financial year select menu

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>

 

David Carr

David Carr

For the past 12 years, I’ve been developing applications for the web using mostly PHP. I do this for a living and love what I do as every day there is something new and exciting to learn.

In my spare time, the web development community is a big part of my life. Whether managing online programming groups and blogs or attending a conference, I find keeping involved helps me stay up to date. This is also my chance to give back to the community that helped me get started, a place I am proud to be apart of.

Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie, staying in playing games on the sofa or planning a trip to someplace I’ve never been before.