user4316754 user4316754 - 1 year ago 55
PHP Question

Put an Echo inside the For Loop

I have seen all similar answers and they all tell us the same thing. But You do need a loop inside echo like a date selector with time?
Is there a nice way with just 1 For Loop to do all date & time?

I am so sorry, my code indeed is terrible and functions just didn't cross my mind. I'll keep that in mind, all perfect answers thankyou!

Answer Source

Create function and then use it:

function options($start, $end, $name, $label = '') {
  echo "{$label}<select name='$name'><option></option>";
  for($x = $start; $x <= $end; $x++) { echo "<option value='$x'>$x</option>"; }
  echo "</select>";

options(1, 31, 'day', 'Date:');
options(1, 12, 'month', '/');
options(2012, 2022, 'year', '/');
options(0, 23, 'hour', 'Time:');
options(0, 59, 'minute', ':');