Hi I wish to display a message letting visitor know that server is busy backing up data between 18.10 and 21.10 (6:10 - 9:10) every working day, using php. I have written this code but I can not figure out how to incorporate minutes as well as hours. ( At present this only checks the hour). I am sure there are better ways of writing even this code. Can some one please help me. $tim = localtime(time(),true); if( (date ("l") =="Monday" || date ("l") =="Tuesday" || date ("l") =="Wednesday" || date ("l") =="Thursday" || date ("l") =="Friday" ) && !($tim['tm_hour']>=18 && $tim['tm_hour']<21 )) { echo "correct stock display"; } else { echo "NOT correct stock display"; } Code (markup):
Hi can some one plz tell me if I am on the right track: I am not at all an expert on php. I have used todays current hour and minutes and converts them to 24 hour number. then I have simply compared it with numeric 18.166 (18:10 time) and 21.166 (21:10) and if it is between them and on weeks days I will display a message saying backup not allowed. Thank for looking at it.
I think you're getting overly complicated on this. Just to clarify, you want to display one message if it is a weekday, between 18:10 and 21:10? If not, please correct me. Try something like this: $message = 'NOT correct stock display'; switch(date('N')): case 1: //Monday case 2: //Tuesday case 3: //Wed case 4: //Thurs case 5: //Fri $start = mktime(18,10); $end = mktime(21,10); $now = time(); //Current timestamp if($now > $start && $now < $end): $message = 'correct stock display'; endif; break; endswitch; echo $message; PHP: The messages may be backwards, but this should do what you need.
Job done, nice and sweet. I suppose thats the difference between novice and expert. As always when the problem is solves , it looks simple. Many thanks.
Hi As i am a beginner, can you please explain how does it know what day of week it is? I can understand the time check but cant understand how I can call the switch(date('N')): part. Thanks
it know the day of week from this function: date('N') PHP: this is the current day of week (in number). it return (today) weekday number! for example if you run this function (date('N')) at monday you will got 1 if you run it at Tuesday you will got 2 and same.....
Hi just a slight complication. My PHP version is 4.4.2 and therefore can not use date('N') function. is there any easy workaround please? thanks
Change to this: switch(date('w')): PHP: This will still work fine for M-F, But Saturday and Sunday are different, if you ever need to use them. Saturday is 6 and Sunday is 0. With 'N', Saturday is 6 and Sunday is 7.