eldan88 Posted August 25, 2014 Share Posted August 25, 2014 (edited) 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 August 25, 2014 by eldan88 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted August 25, 2014 Share Posted August 25, 2014 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 25, 2014 Share Posted August 25, 2014 Use DatePeriod and DateInterval classes to generate the hourly intervals. Quote Link to comment Share on other sites More sharing options...
CroNiX Posted August 25, 2014 Share Posted August 25, 2014 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.