Skip to main content
Inspiring
February 6, 2012
Answered

setting dynamic default values in a list/menu

  • February 6, 2012
  • 1 reply
  • 2541 views

To save problems with inserting dates into a Mysql database, I have split the input fields on the file insert form into three drop down lists containing the day, month and year.

In order to make it easy for users, I wish to take the current date, split it into day (dd) month (mm) and year (yyyy), and use one of these values as the default option for each of the three fields.

In html you would use :

<option value="2012" selected>2012</option> for the default, but this is a fixed default.

I want it to change depending on the actual year., so that if today is 21-02-2012, (thats how we use dates in the UK)  the three fields default to 21 (dd) 02 (mm)  2012 (yyyy).

I can re-format them into MySql date format mmddyyyy no problem later to insert them into the database

Any ideas?

Or is there a better way to do this?

This topic has been closed for replies.
Correct answer David_Powers

<option value="2012" <?php if (date('Y') == '2012') echo 'selected'; ?>>2012</option>

1 reply

Participating Frequently
February 6, 2012

In ASP/VBScript you would:

<option value=<%=DatePart("yyyy",Now())%> selected><%=DatePart("yyyy",Now())%></option>

In PHP, you could use the getdate() function to return the current date in an array and reference the year using the [year] subscript.

Inspiring
February 7, 2012

That's a good idea. I filled my date options from a snippet, and never thought of typing the code into the options box.

Will work on it.

Thanks

Howard Walker

David_Powers
David_PowersCorrect answer
Inspiring
February 7, 2012

<option value="2012" <?php if (date('Y') == '2012') echo 'selected'; ?>>2012</option>