DDmUSA Posted February 23, 2011 Share Posted February 23, 2011 Hello, I tried to implement some PHP code to enable a web page to show "Dinner" specials versus "Lunch" specials based on the time. The business is located in the Eastern Time Zone, so I only need to routine to work based on Eastern Time (New York). What I am noticing is that the server is processing the lines of code so fast that the command to establish the correct time for the remaining code is not always being acknowledged. This line of code appears to be processing too fast for the remaining lines of code: date_default_timezone_set ( "America/New_York" ); Is there some additional code I can put in place to make sure the correct time is always ascertained? I need the $hourmin variable to always return a combination of hour + minute based on 24-hour time and Eastern Time zone. My code is as follows: <?php $name8 = file_get_contents("test/special8name.txt"); date_default_timezone_set ( "America/New_York" ); $gethour = date("H"); $getminutes = $gettimedate["minutes"]; $gettimedate = date(); $hourmin = $gethour . $getminutes; $currentday = date("l", time()); $currentdate = date("M j, Y"); if ($hourmin < 1500 && $currentday <> "Saturday" && $currentday <> "Sunday") { echo "<span class=\"namesred\">$name8 </span>"; } else if ( $hourmin > 1500 && $hourmin < 2300 && $currentday <> "Saturday" && $currentday <> "Sunday") { echo "<span class=\"namesblue\">$name8 </span>"; } else if ( $currentday == "Saturday" or $currentday == "Sunday") { echo "<span class=\"namesblue\">$name8 </span>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/228552-fetching-the-desired-time-for-a-time-zone/ Share on other sites More sharing options...
DDmUSA Posted February 23, 2011 Author Share Posted February 23, 2011 Hello again... Until I am able to learn the necessary code to fetch the desired time, I decided to change the existing code to have it based on the server time of UTC. The following code might work just fine for my situation, but I would still like to learn how to deal with the shared server environment whereby the time was not always being fetched correctly. I removed the time zone line from the original code and used the following conditional statements: if ($hourmin >= 501 && $hourmin <= 1959 && $currentday <> "Saturday" && $currentday <> "Sunday") { echo "<span class=\"titlered\">Lunch Specials for <br />$currentday "."$currentdate <br /> </span>"; echo $hourmin; } else if (( $hourmin >= 2000 && $hourmin <= 2459 && $currentday <> "Saturday" && $currentday <> "Sunday") or ( $hourmin >=0 && $hourmin <= 459 && $currentday <> "Saturday" && $currentday <> "Sunday")) { echo "<span class=\"titleblue\">Dinner Specials for <br />$currentday "."$currentdate <br /> </span>"; echo $hourmin; } else if ( $currentday == "Saturday" or $currentday == "Sunday") { echo "<span class=\"titleblue\">All-Day Specials for <br />$currentday "."$currentdate <br /> </span>"; echo $hourmin; Quote Link to comment https://forums.phpfreaks.com/topic/228552-fetching-the-desired-time-for-a-time-zone/#findComment-1178446 Share on other sites More sharing options...
jcbones Posted February 23, 2011 Share Posted February 23, 2011 Here is your file, as you posted it, commented the way it is written, there are a few glaring problems with the logic of it. <?php $name8 = file_get_contents("test/special8name.txt"); //retrieve file as string date_default_timezone_set ( "America/New_York" ); //set timezone to Eastern. $gethour = date("H"); //get 24 hour clock "hour". $getminutes = $gettimedate["minutes"]; //there is not an array called $gettimedate set. $gettimedate = date(); //date must have a string format, cannot pass without argument. $hourmin = $gethour . $getminutes; //contantenate the hour and minutes(minutes will be 0, as it will return false on an array index.) $currentday = date("l", time()); //return full text day of week, do not need second argument, as date() returns current timestamp. $currentdate = date("M j, Y"); //3 letter month, 1-31 no leading 0's, 4 number year, on current timestamp. if ($hourmin < 1500 && $currentday <> "Saturday" && $currentday <> "Sunday") { //if hour + minutes is less than 1500, and current dayis greater than/less than Saturday, and currentday is greater than/less than Sunday. echo "<span class=\"namesred\">$name8 </span>"; //echo the file contents in class of namesred. } else if ( $hourmin > 1500 && $hourmin < 2300 && $currentday <> "Saturday" && $currentday <> "Sunday") { //if hour + minutes is greater than 1500, and hour +minutes is less than 2300, and currentday is greater than/less than Saturday, and currentday is greater than/less than Sunday. echo "<span class=\"namesblue\">$name8 </span>"; //echo the file contents in class namesblue } else if ( $currentday == "Saturday" or $currentday == "Sunday") { //if currentday is equal to Saturday, or currentday is equal to Sunday echo "<span class=\"namesblue\">$name8 </span>"; //echo file contents in class namesblue. } ?> Compare the logic to this. <?php $name8 = file_get_contents("test/special8name.txt"); //retrieve file as string date_default_timezone_set ( "America/New_York" ); //set timezone to Eastern. $hourmin = date("Hi"); //get 24 hour clock "hour" and 'minutes'. $currentday = date("l"); //return full text day of week $currentdate = date("M j, Y"); //3 letter month, 1-31 no leading 0's, 4 number year, on current timestamp. if ($hourmin < 1500 && $currentday != "Saturday" && $currentday != "Sunday") { //if hour + minutes is less than 1500, and current day is not Saturday, and currentday not Sunday. $class = 'namesred'; //set class name } else if ( $hourmin > 1500 && $hourmin < 2300 && $currentday != "Saturday" && $currentday != "Sunday") { //if hour + minutes is greater than 1500, and hour +minutes is less than 2300, and currentday is not Saturday, and currentday is not Sunday. $class = 'namesblue'; //set class name } else if ( $currentday == "Saturday" || $currentday == "Sunday") { //if currentday is equal to Saturday, or currentday is equal to Sunday $class = 'namesblue'; //set class name } echo "<span class=\"$class\">$name8 </span>"; //echo file contents, with correct class name. ?> Quote Link to comment https://forums.phpfreaks.com/topic/228552-fetching-the-desired-time-for-a-time-zone/#findComment-1178456 Share on other sites More sharing options...
DDmUSA Posted February 23, 2011 Author Share Posted February 23, 2011 Thanks for explaining my fractured or incorrect logic. Will the time zone command be processed correctly each and every time someone views the page ??? The web hosting server is a shared server with a shared PHP environment. I was noticing that the time being returned was not always Eastern time.... sometimes it was UTC.... the time of the server. Please advise... Quote Link to comment https://forums.phpfreaks.com/topic/228552-fetching-the-desired-time-for-a-time-zone/#findComment-1178515 Share on other sites More sharing options...
DDmUSA Posted February 23, 2011 Author Share Posted February 23, 2011 On the daily specials page, I would like to change the title of the page based on various time scenarios: At 11:00 PM through to 2:59PM the following day for each of the weekdays (Monday through Friday), I would like to display the title: Lunch Specials for Wednesday, February 23, 2011 From 3:00 PM to 10:59 PM for each of the weekdays, I would like to display the title: Dinner Specials for Wednesday, February 23, 2011 On Saturday and Sunday, I would like to display the title: All-Day Specials for Saturday, February 26, 2011 (for example) I think I have the code correct, except for the hour between 11 PM and Midnight where I need to advance the full name for the day of the week to the next day, and the date has to advance by one as well. The code I am starting with is as follows: date_default_timezone_set ( "America/New_York" ); //set timezone to Eastern. $hourmin = date("Hi"); //get 24 hour clock "hour" and 'minutes'. $currentday = date("l"); //return full text day of week $currentdate = date("M j, Y"); //3 letter month, 1-31 no leading 0's, 4 number year, on current timestamp. if ($hourmin < 1500 && $currentday != "Saturday" && $currentday != "Sunday") { //if hour + minutes is less than 1500, and current day is not Saturday, and currentday not Sunday. echo "<span class=\"titlered\">Lunch Specials for <br />$currentday "."$currentdate <br /> </span>"; } else if ( $hourmin > 1500 && $hourmin < 2300 && $currentday != "Saturday" && $currentday != "Sunday") { //if hour + minutes is greater than 1500, and hour +minutes is less than 2300, and currentday is not Saturday, and currentday is not Sunday. echo "<span class=\"titleblue\">Dinner Specials for <br />$currentday "."$currentdate <br /> </span>"; } else if ( $currentday == "Saturday" || $currentday == "Sunday") { //if currentday is equal to Saturday, or currentday is equal to Sunday echo "<span class=\"titleblue\">All-Day Specials for <br />$currentday "."$currentdate <br /> </span>"; } Quote Link to comment https://forums.phpfreaks.com/topic/228552-fetching-the-desired-time-for-a-time-zone/#findComment-1178524 Share on other sites More sharing options...
DDmUSA Posted February 23, 2011 Author Share Posted February 23, 2011 I think this code will achieve my desired result: date_default_timezone_set ( "America/New_York" ); //set timezone to Eastern. $hourmin = date("Hi"); //get 24 hour clock "hour" and 'minutes'. $currentday = date("l"); //return full text day of week $currentdate = date("l F d, Y"); //3 letter month, 1-31 no leading 0's, 4 number year, on current timestamp. if ($hourmin < 1500 && $currentday != "Saturday" && $currentday != "Sunday") { //if hour + minutes is less than 1500, and current day is not Saturday, and currentday not Sunday. $class = 'namesred'; //set class name echo "<span class=\"titlered\">Lunch Specials for <br /> $currentdate <br /> </span>"; } else if ($hourmin >= 2300 && $hourmin <=2359 && $currentday != "Saturday" && $currentday != "Sunday") { //if hour + minutes is less than 1500, and current day is not Saturday, and currentday not Sunday. $tomorrow = date("l F d, Y" , strtotime("+1 days")); echo "<span class=\"titlered\">Lunch Specials for <br /> $tomorrow <br /> </span>"; } else if ( $hourmin > 1500 && $hourmin < 2300 && $currentday != "Saturday" && $currentday != "Sunday") { //if hour + minutes is greater than 1500, and hour +minutes is less than 2300, and currentday is not Saturday, and currentday is not Sunday. $class = 'namesblue'; //set class name echo "<span class=\"titleblue\">Dinner Specials for <br />$currentday "."$currentdate <br /> </span>"; } else if ( $currentday == "Saturday" || $currentday == "Sunday") { //if currentday is equal to Saturday, or currentday is equal to Sunday $class = 'namesblue'; //set class name echo "<span class=\"titleblue\">All-Day Specials for <br />$currentday "."$currentdate <br /> </span>"; } Quote Link to comment https://forums.phpfreaks.com/topic/228552-fetching-the-desired-time-for-a-time-zone/#findComment-1178530 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.