Jump to content

Recommended Posts

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" />';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/254170-time-script/
Share on other sites

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/

Link to comment
https://forums.phpfreaks.com/topic/254170-time-script/#findComment-1303119
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/254170-time-script/#findComment-1303121
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/254170-time-script/#findComment-1303122
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/254170-time-script/#findComment-1303123
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/254170-time-script/#findComment-1303125
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/254170-time-script/#findComment-1303138
Share on other sites

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" />';

Link to comment
https://forums.phpfreaks.com/topic/254170-time-script/#findComment-1303139
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.