Jump to content

Need help creating a select time drop down


eldan88

Recommended Posts

Hey Guys. I am trying to create a time dropdown and increment eat by an an hour until it is lless than or equal to the end time. I am using the DateTime class to accomplish this.

 

Now when I am trying to use a for loop to accompish this it does not work. Can anyone please help me out with this issue?

 

 

Below is the code that I have


 $start_hour = new DateTime("now",new DateTimeZone("America/New_York"));
 $start_hour->setTime(6,00);
 $formatted_start_time = $start_hour->format("H:i:s");


    $end_hour = mktime(11,45);


  for($start_hour; $start_hour <= $end_hour; $start_hour->modify("+60 minutes"));{


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

    echo "<option>{$formatted_start_time}</option>";


   echo "</select>";


 }

Thanks!

Edited by eldan88
Link to comment
Share on other sites

C'mon, you've been doing this long enough to understand that “doesn't work” isn't regarded as a valid problem description.

 

When I run your code with error reporting turned on, I get this message:

Notice: Object of class DateTime could not be converted to int in [path to script] on line 12

And line 12 is this:

for($start_hour; $start_hour <= $end_hour; $start_hour->modify("+60 minutes"));{

Hint: It's the $start_hour <= $end_hour expression. You cannot compare a DateTime object ($start_hour) with an integer ($end_hour). What you can do, though, is compare two DateTime instances. Or you can compare the return value of the getTimestamp() method with another Unix timestamp.

Link to comment
Share on other sites

This code will generate a separate dropdown for each hour since the <select></select> is within the loop. You only want the <option> generated in there. They will also all be filled with the same value since you're using 

"<option>{$formatted_start_time}</option>"

which was created before the loop.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.