Skip to content

Instantly share code, notes, and snippets.

@reitermarkus
Created November 5, 2012 15:18
Show Gist options
  • Save reitermarkus/4017709 to your computer and use it in GitHub Desktop.
Save reitermarkus/4017709 to your computer and use it in GitHub Desktop.
Time-based css with javascript + php.
<!DOCTYPE html>
<html>
<head>
<style>
.night {
background: #000;
color: #fff;
}
</style>
</head>
<body>
<script>
if(window.location.href == window.location.origin + window.location.pathname) {
window.location.href = "?timezone=" + -new Date().getTimezoneOffset()/60;
}
var date = new Date();
var time = date.getHours() + date.getMinutes() / 60;
if(time > 19 || time < 5.5) {
document.body.className+=' night';
}
</script>
<?php
$timezone = $_GET['timezone'];
// castledragmire.com/Posts/Setting_the_time_zone_through_a_numeric_offset
date_default_timezone_set('Etc/GMT'.($timezone<=0 ? '+' : '').(-$timezone));
$time = date("G") + (date("i")/60);
if($time < 5.5 || $time > 19) {
echo "It's nighttime! (".$time.")";
} else {
echo "It's daytime! (".$time.")";
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment