PHP drop down box for selecting a day

Hello All,

I am looking for a bit of help - I have a drop down box which is created using PHP and shows the next 7 days, as follows:

  					<option></option>
  					<option><? echo date('l F jS', strtotime("today")); ?></option>
  					<option><? echo date('l F jS', strtotime("tomorrow")); ?></option>
  					<option><? echo date('l F jS', strtotime("+ 2 days")); ?></option>
  					<option><? echo date('l F jS', strtotime("+ 3 days")); ?></option>
  					<option><? echo date('l F jS', strtotime("+ 4 days")); ?></option>
  					<option><? echo date('l F jS', strtotime("+ 5 days")); ?></option>
  					<option><? echo date('l F jS', strtotime("+ 6 days")); ?></option>
  					<option><? echo date('l F jS', strtotime("+ 7 days")); ?></option>
				</select>

However, due to a change in circumstance I can now only offer my services all day on Tuesdays and Thursday and every other day is Evenings only. I have had a stab at this but my PHP is limited and I haven’t been able to do it as I would wish. I really need the drop down box to look like this:

Wednesday (evening only)
Thursday (all day)
Friday (evening only)
etc…

But still start at the current day so it would say:
Today (evening only)
Tomorrow (all day)
Friday (evening only)

I’m assuming this is possible, but how straightforward is it and could any one give me some pointers?

Thank you all in advance.

I’d do it as a function, and call it from a loop rather than hard-coding each individual day. Something like this:


<?php

function myDate($dy) {
  $daynum = date('w', $dy);
  $suffix = " (evening only)";

  switch($daynum) {
    case 2:  // Tuesday
    case 4:  // Thursday
      $suffix = " (all day)";
      break;
    }
  return '<option>' . date("l F jS", $dy) . $suffix . '</option>';
}

echo '<select name="day">';

$d = strtotime("today");
while ($d < strtotime("+7 days")) {
  echo myDate($d);
  $d += 86400; // number of seconds in a day, to get to next day
  }

echo '</select>';
?>

Function might need to be modified to have a value in the opening ‘option’ tag. Basically it gets the day-number from the date you pass in, sets a default suffix and changes it if it’s one of the qualifying days.

Thank you very much for that @droopsnoot - it worked first time :slight_smile:

I hope you don’t mind me asking this but would it be possible that ‘today’s’ day drops off at a certain time. At the moment people will be able to select the current day all the way up to midnight, it would be ideal if it would drop off at say 17:00.

Is this possible at all?

If not, or you are too busy then don’t worry :slight_smile:

In the main code, you’d need to make a change to this bit:


$d = strtotime("today");

to read something like


$x = date("H", strtotime("today"));
if ($x < 17) { // or whatever hour you want to use as cut-off.
  $d = strtotime("today");
  }
else
  {
  $d = strtotime("tomorrow");
  }

So in theory, we take the hour portion of the current datetime in 24-hour format, see if it’s lower than 17, if it is start the dropdown at today, otherwise start it at tomorrow.