Current code for selecting birthday dates
<label>Birthday</label>
<select name="dob-year" id="dob-year" >
<option value="" disabled>Year</option>
<option value="" disabled>----</option>
...
<option value="1971" {{date("Y", strtotime($friend->birthday)) == '1971' ? 'selected' : ''}}>1971</option>
<option value="1970" {{date("Y", strtotime($friend->birthday)) == '1970' ? 'selected' : ''}}>1970</option>
<option value="1969" {{date("Y", strtotime($friend->birthday)) == '1969' ? 'selected' : ''}}>1969</option>
...
</select>
<select name="dob-month" id="dob-month" >
<option value="" disabled>Month</option>
<option value="" disabled>-----</option>
<option value="01" {{date("F", strtotime($friend->birthday)) == 'January' ? 'selected' : ''}}>01</option>
<option value="02" {{date("F", strtotime($friend->birthday)) == 'February' ? 'selected' : ''}}>02</option>
...
</select>
<select name="dob-day" id="dob-day" >
<option value="" disabled>日</option>
<option value="" disabled>---</option>
<option value="01" {{date("d", strtotime($friend->birthday)) == '01' ? 'selected' : ''}}>01</option>
<option value="02" {{date("d", strtotime($friend->birthday)) == '02' ? 'selected' : ''}}>02</option>
...
</select>
What I want to do
is to add {{$friend->birthday == null ? 'selected' : ''}}
like below in each Year/Month/Day tags
<select name="dob-year" id="dob-year" >
<option value="" disabled>Year</option>
<option value="" {{$friend->birthday == null ? 'selected' : ''}} disabled>----</option>
...
</select>
The problem This a weird one I cant seem to find the issue that is causing this. Ive disabled all javascript and double checked what is being returned from controller too but cannot find the source of the issue.
The default selected values of Year/Month/Day is always 1970/01/01 even if I hard code selected
on other options. For example <option value="" selected>Year</option>
would have it only select 1970.
If I delete the whole 1970 option tag only then everything works fine. So for this to work I had to delete 1970 option tag, 01 January option tag and 01 day option tag. Which is no use...
Does anyone have an idea what could be causing this?
CodePudding user response:
This is happening due to the format you are giving to strtotime()
function, when giving any unsupported format to
echo date('Y/d/m',strtotime("20/03/03"));
echo date('Y:d:m',strtotime(null))
it will return you default value which is `1970/01/01`
you are passing either wrong format or null as parameter to strtotime()
CodePudding user response:
If what Muhammed says is true, then do this instead
const dob = "1971/02/01" // "<?= $friend->birthday) ?>";
const [yyyy,mm,dd] = dob.split("/")
document.getElementById("dob-year").value=yyyy;
document.getElementById("dob-month").value=mm;
document.getElementById("dob-day").value=dd;
<label>Birthday</label>
<select name="dob-year" id="dob-year" >
<option value="" disabled>Year</option>
<option value="" disabled>----</option>
<option value="1971">1971</option>
<option value="1970">1970</option>
<option value="1969">1969</option>
</select>
<select name="dob-month" id="dob-month" >
<option value="" disabled>Month</option>
<option value="" disabled>-----</option>
<option value="01" 01</option>
<option value="02">02</option>
</select>
<select name="dob-day" id="dob-day" >
<option value="" disabled>日</option>
<option value="" disabled>---</option>
<option value="01">01</option>
<option value="02">02</option>
</select>