zenix Posted October 11, 2008 Share Posted October 11, 2008 Hi, I am learning php and have been doing alright figuring things out myself...until this. I need a scrpt that will determine what time it is and post a message accordingly. For instance, if it's morning I'd like the page to read, Good Morning. The same for afternoon and evening. I have attempted multiple if/else statements and case switch statements. I MUST be over looking somehting. Here is the case statement: $time = date("G i" ,time()); echo "<br>"; echo "$time"; switch ($time) { case 0: if ($time > 1200) echo "<br>"; echo "Good Afternoon!<br>"; break; case 1: if ($time > 1200 || $time < 1659) echo "<br>"; echo "Good Afternoon!<br>"; break; case 1: if ($time > 1700) echo "<br>"; echo "Good Evening!<br>"; break; } To me it looks as thoughit should work....but what do I know? Apparently not much about this. Any guidence would be very much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/127968-simple-time-function/ Share on other sites More sharing options...
JasonLewis Posted October 11, 2008 Share Posted October 11, 2008 Remove the switch, you don't need that. Switches take the argument variable and if it is equal to 0 it'll run case 0, if it's equal to 1 it'll run case 1 and so on... Try something like this: $time = date("G i" ,time()); echo "<br>"; echo "$time"; if ($time > 1200){ echo "<br>"; echo "Good Afternoon!<br>"; }elseif ($time > 1200 || $time < 1659){ echo "<br>"; echo "Good Afternoon!<br>"; }elseif ($time > 1700){ echo "<br>"; echo "Good Evening!<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/127968-simple-time-function/#findComment-662642 Share on other sites More sharing options...
zenix Posted October 11, 2008 Author Share Posted October 11, 2008 Remove the switch, you don't need that. Switches take the argument variable and if it is equal to 0 it'll run case 0, if it's equal to 1 it'll run case 1 and so on... Try something like this: $time = date("G i" ,time()); echo "<br>"; echo "$time"; if ($time > 1200){ echo "<br>"; echo "Good Afternoon!<br>"; }elseif ($time > 1200 || $time < 1659){ echo "<br>"; echo "Good Afternoon!<br>"; }elseif ($time > 1700){ echo "<br>"; echo "Good Evening!<br>"; } Thanks an awful lot for your input! Just to experiment, I copied and pasted your code into a new page, so I could see how it would work. My next step of course is to figure out how each part works so I learn better. Anyway, no matter what happens it still says good Afternoon. I have changed the time on the computer and the page reflects this change, but it still only says good morning. I also tried changing the if($time > 1200) to read if($time < 1200)...and changed the first echo to Good Morning. Now it only displays good morning. Thanks for the intro to elseif, basic to you I'm sure, but looks like a handy tool! Quote Link to comment https://forums.phpfreaks.com/topic/127968-simple-time-function/#findComment-662650 Share on other sites More sharing options...
JasonLewis Posted October 11, 2008 Share Posted October 11, 2008 Okay I looked at your if() statements and they're a bit muddled. It will always say Good Morning if the time is greater then 12. This is because it stops at the first if(), you need to add something else in there. Take a look at this (I've commented it): <?php $time = (int) date("Gi" ,time()); echo "<br>"; echo "$time"; if($time > 0 && $time < 1159){ //Check if the current time is greater then 0 and also if it is less than 1159 echo "<br>"; echo "Good Morning!<br>"; }elseif($time > 1200 && $time < 1659){ //If it wasn't the above, now check if it is greater than 12 but also check to see if it is less than 1659 echo "<br>"; echo "Good Afternoon!<br>"; }elseif($time > 1700 && $time < 2400){ //This can just be }else{ but it doesn't matter. If the time is greater than 1700 and less than 2400 it must be evening echo "<br>"; echo "Good Evening!<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/127968-simple-time-function/#findComment-662662 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.