Reputation: 319
Is there a simple if statement to default select the correct date from the dropdown? so for example if the date is 02/11/16
then it will default select
01/28/16
Basically what I'm asking for is an if statement that default selects the drop-down that the date is within
<?php
date_default_timezone_set('US/Eastern');
$currenttime = date('d:m:y:h:i:s A');
list($day,$month,$year) = split(':',$currenttime);
$currentdate = "$month/$day/$year";
?>
<form>
<select>
<option>01/14/16</option>
<option>01/28/16</option>
<option>02/14/16</option>
///the list goes on for ages so i saved time and cropped out the rest of the dates.
</select>
</form>
Upvotes: 1
Views: 581
Reputation: 780879
Put all the dates in an array, and loop through them. Then you can test them and decide whether the current date is in the billing period.
$dates = array('16-01-14',
'16-01-28',
'16-02-14',
...);
$currentdate = date('y-m-d');
?>
<form>
<select>
<?php
foreach ($date as $i => $d) {
if ($currentdate >= $d && ($i == count($dates)-1 || $currentdate < $dates[$i+1])) {
$selected = "selected";
} else {
$selected = "";
}
list($year, $month, $day) = explode('-', $d);
echo "<option $selected>$month/$day/$year</option>";
}
?>
</select>
</form>
I changed the format of $currentdate
to y-m-d
so that they can be compared as strings to see if the date is in a range.
As it loops through the list of dates, it tests whether the current date is between that date and the next date in the array (or it's the last date in the array). If it is, it adds the selected
attribute to the <option>
.
Upvotes: 1