Current year session echo in dropdown box auto selected

I want to create a dropdown box, in PHP and HTML, where the starting year will be 2020 and the current year will be 2023 [auto]. the year session will append in select box like:

2022-2023 // selected
.. auto for 2024
  <option selected>2022-2023</option>

I don’t know, how I code, please help to create this in PHP and HTML

Look up these things
comparison operators

OK times up. No really I hope you were able to figure some things out.

echo '<select name="year">
	<option value="">-Select-</option>';
	foreach(range("2022",date("Y")+1) as $year){	
		$selectedyr = (date("Y") == $year ? ' selected="selected"' : '');
		echo '<option value="'.$year.'"'.$selectedyr.'>'.$year.'</option>';
echo '</select>';
1 Like

yes but my question is like this if the current year is 2023 then select option will 2022-2023, same if this current year is 2024 then it should be 2023-2024

Well how is the current year defined in my example and how did I get the value for the next year? It was the date("Y")+1. So for the range from and to values you would define them as you want them to be.

range(date("Y")-1, date("Y"))

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.