Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Time-of-day based web design

Featured Replies

Hi all,

 

I need to build a website where the appearance/background image varies depending on the time of day (say, the user's computer clock). There'll only be a few variations: morning, afternoon, evening. Is this simple to do, and if so can anyone provide any insight to help me out?

 

Many thanks

I was thinking of something related to this the other day. Not too difficult - use PHP to determine the time and then serve up the relevant code.

I agree, PHP is your best bet. You could do something like this... I hope this works for you. Good Luck

 

 

<?php

 

$day = date("G");

 

switch($day)

{

case $day >= 8 && $day <= 9:

include'styleSheet1.css';

break;

 

case $day >= 10 && $day <= 12:

include'styleSheet2.css';

break;

 

}

 

?>

  • 2 weeks later...

Here's what I'd do for a simple design difference between times (like the background image that you mention).

 

Have a PHP function like this to return a word representing the hour of the day:

 

function getHourClass() {
$hour = date('G');

switch(true) {
	case ($hour<12):
		return 'morning';

	case ($hour>11 && $hour<18):
		return 'afternoon';

	case ($hour>17):
		return 'evening';
}
}

 

Then in your markup, have an include to insert this word as a class on your body tag like this:

 

<body class="<?= getHourClass(); ?>">

 

Which means you can then refer to the differences in your CSS file like this:

 

body.morning {
/* morning css here */
}

body.afternoon {
/* afternoon css here */
}

body.evening {
/* evening css here */
}

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.