Kevin McLean Posted January 16, 2009 Share Posted January 16, 2009 I was trying to write a simple script which would change the the layout according day time, but it does not work. Please, could somebody help me to fix the problem? PHP Code: <? $hour = date(”H”); if ($hour > 9 || $hour < 18) { include('eldia/day_header.php'); } elseif ($hour > 18 || $hour < 9) { include('lanoche/night_header.php'); } else { echo "Заклинания не работают!"; } include('main.php'); <? $hour = date(”H”); if ($hour > 9 || $hour < 18) { include('eldia/day_footer.php'); } elseif ($hour > 18 || $hour < 9) { include('lanoche/night_footer.php'); } else { echo "Заклинания не работают!"; } ?> There are only 2 layouts - one for day and one for night. And one more question how to count time according not the server time, but accroding a users' time? Thank you Link to comment https://forums.phpfreaks.com/topic/141043-changing-layout-according-day-time/ Share on other sites More sharing options...
chronister Posted January 16, 2009 Share Posted January 16, 2009 <?php $hour = date('G'); // changed to 24 hour format with no leading 0 // assuming daytime as between 8am - 5pm if ($hour >= 8 && $hour <= 17) { echo ' it is daytime'; } else { echo 'it is night time'; } ?> I am assuming that your considering "daytime" between 8am - 5pm... but change it how you want. In PHP there is no method I know of that can detect a user's timezone. PHP is serverside, and done by the time the user sees anything. Take a look at javascript though and you may be able to combine it with ajax to accomplish it. Nate Link to comment https://forums.phpfreaks.com/topic/141043-changing-layout-according-day-time/#findComment-738225 Share on other sites More sharing options...
dropfaith Posted January 16, 2009 Share Posted January 16, 2009 i use this to change some layouts i have so here it is full php to change stylesheets based on server time.. you will need to edit the location of the stylesheets and times accordingly <link rel="stylesheet" type="text/css" media="screen" href="<?php $hour = date("H"); //PHP date() function fetching the current hour on local machine if ($hour < //if time is below noon(12pm) { echo "/template/sunrise/style.css"; } elseif ($hour < 12) //if it is afternoon(greater than 12 but below 5) { echo "/template/grass/style.css"; } elseif ($hour < 17) //if it is afternoon(greater than 12 but below 5) { echo "/template/winter/style.css"; } elseif ($hour < 20) //if it is afternoon(greater than 12 but below 5) { echo "/template/sunset/style.css"; } else { echo "/template/courthouse/style.css"; // if it is evening or night } ?>" /> javascript to change by user time if thats what your after <SCRIPT LANGUAGE="JavaScript"> <!-- Begin function getCSS() { datetoday = new Date(); timenow=datetoday.getTime(); datetoday.setTime(timenow); thehour = datetoday.getHours(); if (thehour > 20) display = "tree_twilight.css"; else if (thehour > 17) display = "tree_sunset.css"; else if (thehour > 14) display = "tree_afternoon.css"; else if (thehour > 11) display = "tree_noon.css"; else if (thehour > 7) display = "tree_morning.css"; else if (thehour > 4) display = "tree_sunrise.css"; else if (thehour > 1) display = "tree_twilight.css"; else display = "tree_sunset.css"; var css = '<'; css+='link rel="stylesheet" href=' + display + ' \/'; css+='>'; document.write(css); // End --> } </script> <script language="javascript">getCSS();</script> Link to comment https://forums.phpfreaks.com/topic/141043-changing-layout-according-day-time/#findComment-738248 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.