Style your Drupal theme according to day or night time

Here's a simple snippet to allow you to style your theme differently for day or night time. Place the following code into your theme's template.php file.

/**
* Implements template_preprocess_html().
*/
function YOUR_THEME_preprocess_html(&$vars) {
  // Day / night background theming.
  $hour = date('G');
  $theme = 'day';
  if ($hour <= 6) {
    $theme = 'night';
  }
  elseif ($hour >= 6 && $hour <= 18) {
    $theme = 'day';
  }
  elseif ($hour >= 18) {
    $theme = 'night';
  }
  $vars['classes_array'][] = $theme;
}

Clear your Drupal caches from admin/config/development/performance

Now your <body> tag should have the CSS class 'day' or 'night' depending on the current server time.

An example CSS code would look like:

body.day {
  background: #fff;
  color: #000;
}
body.night {
  background: #000;
  color: #fff;
}

Add new comment