The following code segment (line-numbered for convenience only), is intended to produce a different result when it's run on a weekday, a Saturday, or a Sunday, and, if it runs on a weekday, a different result if it's run before 8:30PM or after.
The problem I'm having is on line 19. If the function runs on a weekday, before 8:30PM, the function is_evening() returns false, as it should, but the code in the if statement (lines 20-24) runs anyway (i.e. it echos the text of the file 'evening.txt').
When I rewrite the whole thing to avoid the switch statement, using sequence of if/elseif/else statements, it works as expected. But why is it not working correctly here?
Can anyone tell me where the error is in the code?
Thanks.
Richard
Code: Select all
1 function is_evening() {
2 date_default_timezone_set('America/New_York');
3 return (date("G") * 60 + date("i")) > (20*60 + 30);
4 }
5
6 date_default_timezone_set('America/New_York');
7 $this_day = strtolower(date("l"));
8
9 switch ($this_day) {
10 case "saturday":
11 case "sunday":
12 if (!file_exists("$this_day.txt")) :
13 echo "Error: could not find file '$this_day.txt'.";
14 else :
15 echo file_get_contents("$this_day.txt");
16 endif;
17 break;
18 default:
19 if (is_evening()) :
20 if (!file_exists("evening.txt")) :
21 echo "Error: could not find file 'evening.txt'.";
22 else :
23 echo file_get_contents("evening.txt");
24 endif;
25 else :
26 $filename = date("Ymd.\t\x\t") . ".txt";
27 if (file_exists($filename)) :
28 echo make_table(file_get_contents($filename));
29 elseif (!file_exists("morning.txt")) :
30 echo ("Error: could not find file 'morning.txt'");
31 else :
32 echo (file_get_contents("morning.txt"));
33 endif;
34 endif;
35 break;
36 }