I have a birthdate field which is optional after inserting a record into clients table.
the default value will is “000-00-00” after insert.
Now, I want to edit the record in the table and would like to get the value of the birth_date field into three dropdown list.
The three dropdown list are Day, Month, and Year.
If there is no birthdate the selected value of the three dropdown should be “Day”, “Month”, and “Year”.
I tried the following code and don’t know if this is the best way.
$dateValue = '0000-00-00';
$year = explode("-", $dateValue);
if ($year[0] == "0000") {
$selected_year = "Year";
$selected_month = "Month";
$selected_day = "Day";
}else{
$selected_year = date("Y",strtotime($user['birth_date']));
}
If there is a better way for this, please let me know.
btw, I’m using codeigniter.
I think you are doing your test too late, it is much simpler to do it directly on $dateValue. Also, you can simplify the code by making use of the list function:-
Gives the following output:-
Changing the first line to:-
Gives:-
Personally, I would hide this all away in a function to improve readability:-
Then use it like this:-