esoteric Posted January 1, 2012 Share Posted January 1, 2012 Hi, Im trying to create a simple script that will change the style sheet depending on the time of day. I have the following but it doesn't seem to be working. The stylesheets work fine when not used in the code (if i just add it to the head section) Appreciate any help <? // timezone date_default_timezone_set('GMT'); // check time $time = date("His"); // 6AM to 6PM $day_start = '060000'; $day_end = '180000'; // 6PM to 6AM $night_start = '180001'; $night_end = '055959'; // time of day if (($time >= $day_start) && ($time <= $day_end)) { // it is daytime // echo 'day'; echo '<link rel="stylesheet" media="screen" type="text/css" href="css/wc_parallax_day.css" />'; } if (($time >= $night_start) && ($time <= $night_end)) { // it is nighttime // echo 'night'; echo '<link rel="stylesheet" media="screen" type="text/css" href="css/wc_parallax_day.css" />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254170-time-script/ Share on other sites More sharing options...
SergeiSS Posted January 1, 2012 Share Posted January 1, 2012 I think you misunderstand something. What are you comparing?... Numbers, strings? In your case all variables are compared as numbers. $night_start = '180001'; $night_end = '055959'; if (($time >= $night_start) && ($time <= $night_end)) // your condition is changed to if( $time >= 180001 and $time <= 55959 ) What do you think - is it possible? I'm not sure You need special functions like strtotime() or functions from DateTime object. Look for help here http://www.php.net/manual/en/ Quote Link to comment https://forums.phpfreaks.com/topic/254170-time-script/#findComment-1303119 Share on other sites More sharing options...
QuickOldCar Posted January 1, 2012 Share Posted January 1, 2012 Is another thing to consider. The visitors of your site will be on their own times, this script would only do your server time. You would need to use javascript and attempt to detect their location, and use their timezone. If didn't do that it will never be right for many people. Quote Link to comment https://forums.phpfreaks.com/topic/254170-time-script/#findComment-1303121 Share on other sites More sharing options...
esoteric Posted January 1, 2012 Author Share Posted January 1, 2012 Im trying to get the current time, so lets says its 7 oclock, the value of time would then be (in 24hr, hour,min,sec) 190000 I then want to see what if this value is higher than day_start but lower than day_end, if not then it should be higher than night_start but lower than night_end. Why would this need a special function? Shouldn't it just be a standard check? Thank you for the help. Quote Link to comment https://forums.phpfreaks.com/topic/254170-time-script/#findComment-1303122 Share on other sites More sharing options...
esoteric Posted January 1, 2012 Author Share Posted January 1, 2012 Is another thing to consider. The visitors of your site will be on their own times, this script would only do your server time. You would need to use javascript and attempt to detect their location, and use their timezone. If didn't do that it will never be right for many people. Yes i understand that, im forcing it as GMT at the moment, i will play about with it after i have the basics working. Thanks for the reply. Quote Link to comment https://forums.phpfreaks.com/topic/254170-time-script/#findComment-1303123 Share on other sites More sharing options...
SergeiSS Posted January 1, 2012 Share Posted January 1, 2012 Why would this need a special function? Shouldn't it just be a standard check? What do you think - if different times could be compared as strings, why some function were created? I mean strtotime(), DateTime object and it's functions. Of course, times are compared as numbers, somewhere inside PHP... But YOU don't perform such transformation in your code. Quote Link to comment https://forums.phpfreaks.com/topic/254170-time-script/#findComment-1303125 Share on other sites More sharing options...
QuickOldCar Posted January 1, 2012 Share Posted January 1, 2012 For the timezone detection, this works well. http://www.onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/ Quote Link to comment https://forums.phpfreaks.com/topic/254170-time-script/#findComment-1303130 Share on other sites More sharing options...
SergeiSS Posted January 1, 2012 Share Posted January 1, 2012 Request to moderators: change, please, my previous answer in this topic. My answer looks like some kind of internal quoted text Quote Link to comment https://forums.phpfreaks.com/topic/254170-time-script/#findComment-1303131 Share on other sites More sharing options...
esoteric Posted January 1, 2012 Author Share Posted January 1, 2012 I tried the function but i don't understand the out put, at 20:07 the output without using strtotime is 200700 (which i what i want), but when i use the function it makes it 1325448448 Quote Link to comment https://forums.phpfreaks.com/topic/254170-time-script/#findComment-1303135 Share on other sites More sharing options...
SergeiSS Posted January 1, 2012 Share Posted January 1, 2012 I tried the function but i don't understand the out put, at 20:07 the output without using strtotime is 200700 (which i what i want), but when i use the function it makes it 1325448448 What is not clear? It's written about returned value, in the help the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied Another question: did you try DateTime? http://ru.php.net/manual/en/book.datetime.php Quote Link to comment https://forums.phpfreaks.com/topic/254170-time-script/#findComment-1303138 Share on other sites More sharing options...
Pikachu2000 Posted January 1, 2012 Share Posted January 1, 2012 Since there are only 2 choices, this can be easily done with a simple ternary statement, without repetition. I made the assumption that the name of the stylesheet should change depending on the time of day. In the code above, you had the same stylesheet specified for both results. <?php date_default_timezone_set('GMT'); $time = date("His"); $sheet = $time >= 060000 && $time <= 180000 ? 'day' : 'night'; echo '<link rel="stylesheet" media="screen" type="text/css" href="css/wc_parallax_' . $sheet . '.css" />'; Quote Link to comment https://forums.phpfreaks.com/topic/254170-time-script/#findComment-1303139 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.