eldan88 Posted April 6, 2014 Share Posted April 6, 2014 Hey Guys. I am trying to out put a list of times in a while loop and break when it reaches the closing time. When I do a simple comparision it shows whether its true or false... How ever the comparison will not work in a while loop it will not work. I have tried every single method I can possibly think of and it just won't loop. Do you guys have any suggestions on how I can make this work?? Below is my code. Thanks! <?php $date = new DateTime("now"); $date->setTimezone(new DateTimeZone("America/New_York")); $date->setTime($date->format('G'), ceil($date->format('i') / 15) * 15); // Rounds the minutes to the nearest quarter of an hour. $closing_time = new DateTime("10:00"); while($date > $closing_time) { echo $date->format('g:i a') ."<br>" ; // Only displays once $date->modify( "+15 minutes" ); if($date > $closing_time) { break; } } Quote Link to comment Share on other sites More sharing options...
denno020 Posted April 6, 2014 Share Posted April 6, 2014 Shouldn't your while be $date < $closing_time Quote Link to comment Share on other sites More sharing options...
eldan88 Posted April 6, 2014 Author Share Posted April 6, 2014 Hey Denno020. Yes you are right. Didn't notice it. But even with the $date < $closing time it still won't display anything at all?? Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted April 6, 2014 Solution Share Posted April 6, 2014 if you ran this code past 10:00 am, the $closing_time you are using, it won't display anything. if you meant 10:00 pm, you would need to include that when you create the $closing_time value. Quote Link to comment Share on other sites More sharing options...
eldan88 Posted April 6, 2014 Author Share Posted April 6, 2014 Hey mac_gyver thank you so much for your response. I thought that if I pass the time zone, the parser will automatically know whether or not its AM or PM. I realized in order to calculate that dynamically I need to pass in the hour in the 24 hour format. That is what got me really confused. I went ahead and changed the whole format into 24 hour format. Instead of $date->format('g') (which is 12 hour format) i changed it to $date->('G') and instead of passing $closing_time = new DateTime("10:00"); i passed in $closing_time = new DateTime("22:00"); Now it works. I will copy and paste my code just in case anyone else is having this issue. Thank you once again! <?php $date = new DateTime("now", new DateTimeZone("America/New_York")); //Lined number 4 below $date->setTime($date->format('G'), ceil($date->format('i') / 15) * 15); // Rounds the minutes to the nearest quarter of an hour. $closing_time = new DateTime("22:00", new DateTimeZone("America/New_York")); while( $date < $closing_time ) { echo $date->format('g:i a') ."<br>" ; // Only displays once $date->modify( "+15 minutes" ); if($date > $closing_time) { break; } } 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.