Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Los_Angeles' for 'PDT/-7.0/DST' instead in /home/hemmi3/public_html/blog/inc/items/model/_itemquery.class.php on line 628
Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Los_Angeles' for 'PDT/-7.0/DST' instead in /home/hemmi3/public_html/blog/inc/items/model/_itemquery.class.php on line 628
| « B2evolution 1.10.3 & 2.4.0 HOWTO: use an external SMTP mailserver | New item in blogroll » |
Conditional CSS through PHP
Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Los_Angeles' for 'PDT/-7.0/DST' instead in /home/hemmi3/public_html/blog/inc/sessions/model/_hit.class.php on line 901
Wouldn't it be nice to be able to change the skin's appearance based on some condition. In this tip & trick I'm going to change the colour of this post to red at daytime and blue at night. Times are local CET (Europe).
The nice thing about PHP is that you can deploy it everywhere in the page. So why not within the CSS file.
First you want to point to this special file. Somewhere in the header (for B2evo that's ../blogs/skins/YOURSKIN/_html_header.inc.php) make this call:
<link rel="stylesheet" href="phpstyle.php" type="text/css" />
Every PHP statement must be within a PHP file. We create this phpstyle.php file and start it with:
<?php header("Content-type: text/css"); ?>
That's about it. The file acts as any other CSS file, but since it's a PHP file we can interweave (we say 'lard' in Holland) with any thinkable condition. It's like serverside JavaScript.
On with the task I've set. Let's call the server time:
$time_at_server = date(G);
$time_in_europe = $time_at_server + 9;
Now the condition:
if ($time_in_europe >= 6 && $time_in_europe < 18 )
{
// It's daytime
$sun_is_shining = 1;
}
else
{
// it's night
$sun_is_shining = 0;
};
This is the complete phpstyle.php:
<?php header("Content-type: text/css"); ?>
<?php
$time_at_server = date(G);
$time_in_europe = $time_at_server + 9;
// Found a bug. This is the fix:
if ( $time_in_europe > 23 )
{
$time_in_europe = $time_in_europe - 24;
}
if ( $time_in_europe >= 6 && $time_in_europe < 18 )
{
// It's daytime
$sun_is_shining = 1;
}
?>
.day_or_night p {
<?php
if ( $sun_is_shining )
{
echo ( "color: red; \n" );
}
else
{
echo ( "color: blue; \n" );
}
// output
/**
* .day_or_night p {
* color: red;
* }
*
*/
?>
}
I've started this post with <div class=" day_or_night"> and ended it with </div>.
That's it. I Hope you like it. Can you think of some other uses for this conditional css statements? Leave a comment if you do.
Have fun.
Trackback address for this post
1 comment
nice @+#, neat explanation.. thank you Warning: mktime() [function.mktime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Los_Angeles' for 'PDT/-7.0/DST' instead in /home/hemmi3/public_html/blog/inc/_core/_misc.funcs.php on line 870
Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Los_Angeles' for 'PDT/-7.0/DST' instead in /home/hemmi3/public_html/blog/inc/_core/_misc.funcs.php on line 981
28/03/08 @
Warning: mktime() [function.mktime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Los_Angeles' for 'PDT/-7.0/DST' instead in /home/hemmi3/public_html/blog/inc/_core/_misc.funcs.php on line 870
Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Los_Angeles' for 'PDT/-7.0/DST' instead in /home/hemmi3/public_html/blog/inc/_core/_misc.funcs.php on line 981
20:50