RickyF Posted April 8, 2007 Share Posted April 8, 2007 Hey everyone, It is possible for PHP to display something within a certain time range - using the server time, e.g. if the time was between 8am and 9pm, something is shown, if not then something else is shown (talking in semi php, heh). If so could anyone provide some help or even better the code for it? Looked all over the net, cant find anything about this. Any help is much appreciated. Thanks! Quote Link to comment Share on other sites More sharing options...
Zaid Posted April 8, 2007 Share Posted April 8, 2007 Hey everyone, It is possible for PHP to display something within a certain time range - using the server time, e.g. if the time was between 8am and 9pm, something is shown, if not then something else is shown (talking in semi php, heh). If so could anyone provide some help or even better the code for it? Looked all over the net, cant find anything about this. Any help is much appreciated. Thanks! use Date function to take out the hour only i.e. date('h'); , assign that to a variable and if statement to do whatever you want Quote Link to comment Share on other sites More sharing options...
Dragen Posted April 8, 2007 Share Posted April 8, 2007 I've never done this before but something like <?php $time = date ("G"); if ($time >= 8 && $time <= 21){ //code to be implemented here } else { //code to be shown when not between 8am and 9pm } ?> There's probably a better way of formatting the if statement as well.. Quote Link to comment Share on other sites More sharing options...
Zaid Posted April 8, 2007 Share Posted April 8, 2007 I've never done this before but something like <?php $time = date ("G"); if ($time >= 8 && $time <= 21){ //code to be implemented here } else { //code to be shown when not between 8am and 9pm } ?> There's probably a better way of formatting the if statement as well.. seems ok to me Quote Link to comment Share on other sites More sharing options...
Dragen Posted April 8, 2007 Share Posted April 8, 2007 seems ok to me cool.. my first proper attempt at helping someon else with php Quote Link to comment Share on other sites More sharing options...
RickyF Posted April 8, 2007 Author Share Posted April 8, 2007 Thanks for your replys! and thank you Dragen for providing a code! Do you know how you would add the ability, so that if the date is for example, christmas day... the code echos a certain thing? e.g., if time is 8am - 9pm its on, if not its off, and the date is 12/25 or new years day 01/01, regardless of the times its off. Hope that makes sense Im sure it can easily added to the script written by Dragen - i dont have the knowledge to do so my self yet. Heres the code i have edited slightly: <?php $time = date ("G"); if ($time >= 8 && $time <= 21){ echo "Online"; } else { echo "Offline"; } ?> Thanks for any further help! Quote Link to comment Share on other sites More sharing options...
Dragen Posted April 8, 2007 Share Posted April 8, 2007 well the date ("G"); only reads the time in 24 hour format. I guess this might work: <?php $date = date ("nd"); $time = date ("G"); //checks if time is between 8am and 9pm if ($time >= 8 && $time <= 21){ //code to be implemented here } else { //code to be shown when not between 8am and 9pm } //checks if christmas day if ($date == 1225){ //code to be implemented if xmas } else { //code to be implemented if not xmas } ?> not sure if I've got the $date == 1225 part quite right... Quote Link to comment Share on other sites More sharing options...
Zaid Posted April 8, 2007 Share Posted April 8, 2007 Thanks for your replys! and thank you Dragen for providing a code! Do you know how you would add the ability, so that if the date is for example, christmas day... the code echos a certain thing? ........ it's christmas already? where are my presents? Quote Link to comment Share on other sites More sharing options...
RickyF Posted April 8, 2007 Author Share Posted April 8, 2007 Thanks again Dragen, how ever could you make it so that... If its not between 8am - 9pm or if its christmas, off is shown, otherwise on is shown? Otherwise the code wouldnt work properly as required. Thanks! Quote Link to comment Share on other sites More sharing options...
Dragen Posted April 8, 2007 Share Posted April 8, 2007 <?php $date = date ("nd"); $time = date ("G"); //checks that time is either earlier than 8am or later than 9pm, or xmas if ($time < 8 || $time > 21 || $date == 1225){ //code to be implemented here } else { //code to be shown between 8am and 9pm on days that aren't xmas } ?> Again, not sure if it'll work. I guess you could also seperate the $date = date ("nd"); part to make it easier to read, by using $date = date ("n,d"); and replacing $date == 1225 with $date == 12,25 simply seperating the month and the day. please note that I haven't tried this and you should test it with todays date or something to check that it works.. Quote Link to comment Share on other sites More sharing options...
RickyF Posted April 8, 2007 Author Share Posted April 8, 2007 Thats exactly how i need it, however the date check doesnt seem to work, i tried: <?php $date = date ("nd"); if ($date == 0408) { echo "it works"; } else { echo "it doesnt work"; } ?> i tried the date as 0408 and 48, which i assume one of those is correct for todays date? And it just kept showing it didnt work. FYI, my server is in UK date format, DD/MM/YYYY, if that makes a difference. Thanks! Quote Link to comment Share on other sites More sharing options...
Dragen Posted April 8, 2007 Share Posted April 8, 2007 ah yeah.. try this instead.. I used the wrong month variable <?php $date = date ("md"); $time = date ("G"); //checks that time is either earlier than 8am or later than 9pm, or xmas if ($time < 8 || $time > 21 || $date == 0408){ //code to be implemented here } else { //code to be shown between 8am and 9pm on days that aren't xmas } ?> Quote Link to comment Share on other sites More sharing options...
RickyF Posted April 8, 2007 Author Share Posted April 8, 2007 I have changed the time to 11 so the script works atm, I set the date as today and the new time to 11, which will work untill 11:59pm, how ever its showing online, when it should be showing offline? <?php $date = date ("md"); $time = date ("G"); if ($time < 8 || $time > 23 || $date == 0408) { echo "Offline"; } else { echo "Online"; } ?> Quote Link to comment Share on other sites More sharing options...
Dragen Posted April 8, 2007 Share Posted April 8, 2007 no. up until 12 it should show online. <?php $date = date ("md"); $time = date ("G"); if ($time < 8 || $time > 23 || $date == 0408) { echo "Offline"; //this part is what is shown before 8am and after 11pm } else { echo "Online"; //this is what is shown during the day (between 8am and 11pm) } ?> So until it reaches 12 in the morning it will display online. If you want it to be offline at 11, then you need to specify minutes I think, instead of just hours. so it would be <?php $date = date ("md"); $time = date ("Gi"); if ($time < 800 || $time > 2300 || $date == 0408) { echo "Offline"; //this part is what is shown before 8am and after 11pm } else { echo "Online"; //this is what is shown during the day (between 8am and 11pm) } ?> The 'i' on the $time = date ("Gi"); detects the minutes. Quote Link to comment Share on other sites More sharing options...
RickyF Posted April 8, 2007 Author Share Posted April 8, 2007 Ahh right thanks, will test the date thing tomorrow and let you know - Hopefully it works How do you do 12am, 000 i would guess? And i could add other dates as you did with christmas yes? e.g. if ($time < 800 || $time > 2100 || $date == 1225 || $date == 1010) { Can you make the dates uk format, by changing $date = date ("md"); to $date = date ("dm"); ? Thanks for your help! Quote Link to comment Share on other sites More sharing options...
Dragen Posted April 8, 2007 Share Posted April 8, 2007 12 am would be 0000. To add other dates you would simply do as you've suggested ($time < 800 || $time > 2100 || $date == 1225 || $date == 1010) { Make sure that you use the or symbols ||. the $date = date ("md"); just means that it is month, then day. changing it to $date = date ("dm"); ? would simply mean it would be day then month. look at this page for a list of all the date constants: http://www.wdvl.com/Authoring/Languages/PHP/Date_Time/ Quote Link to comment Share on other sites More sharing options...
RickyF Posted April 9, 2007 Author Share Posted April 9, 2007 Hi, The script is as following, <?php $date = date ("dm"); $time = date ("Gi"); if ($time < 800 || $time > 2100 || $date == 0904) { $status = "offline"; } else { $status = "online"; } ?> I've put todays date, so it should be showing offline, however online is being showed? Quote Link to comment Share on other sites More sharing options...
Dragen Posted April 9, 2007 Share Posted April 9, 2007 what time was it when you checked it? If it was between 8am and 9pm then it will show online. Quote Link to comment Share on other sites More sharing options...
RickyF Posted April 9, 2007 Author Share Posted April 9, 2007 Yes it was between 8am - 9pm, but i need it to show off when its anytime on the set dates :'( Hope you can think of something Quote Link to comment Share on other sites More sharing options...
Dragen Posted April 9, 2007 Share Posted April 9, 2007 oh.. you want it to be offline during the day? that's easy: <?php $date = date ("dm"); $time = date ("Gi"); if ($time >= 800 || $time <= 2100 || $date == 0904) { $status = "offline"; } else { $status = "online"; } ?> Quote Link to comment Share on other sites More sharing options...
RickyF Posted April 9, 2007 Author Share Posted April 9, 2007 Nooo, lol confusing aint it. if its between 8-9 it needs to show as on, but if it was for example xmas day, it should show as offline no matter what time it is. 8-9 on outside 8-9 off 8-9 on xmas off outside 8-9 on xmas off If that makes sense? Quote Link to comment Share on other sites More sharing options...
Dragen Posted April 9, 2007 Share Posted April 9, 2007 oh right, I get ya! sorry I was just getting confused.. I think this should work: <?php $date = date ("dm"); $time = date ("Gi"); if ($date == '0905') { //if $date is equal to xmas $status = "offline"; //this now changes offline } else { if($time >= '800' || $time <= '2100') { $status = "online"; //changed this to set the online status } else { $status = "offline"; //this now changes offline } } ?> Quote Link to comment Share on other sites More sharing options...
RickyF Posted April 9, 2007 Author Share Posted April 9, 2007 B-E-A UTIFUL! It works, now i have to get a calendar out add all mayjor bank holidays to it, fun lol! Thanks for your help dragen your a legend Quote Link to comment Share on other sites More sharing options...
Dragen Posted April 9, 2007 Share Posted April 9, 2007 glad it worked.. I only started learning php about a week ago, so I'm still learning myself! Quote Link to comment Share on other sites More sharing options...
RickyF Posted April 9, 2007 Author Share Posted April 9, 2007 Im amazing at php forms and validation etc, not really needed to learn much else, learnt about times and dates from this script etc. 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.