www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: Redirect Based on Time of Day

  1. #1
    Join Date
    Dec 2007
    Posts
    12

    Redirect Based on Time of Day

    I have this script but i need it to redirect based on for time blocks rather then two. any help would be appreciated
    Code:
    <HTML>
    <HEAD>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function ampmRedirect(dayURL, nightURL)
    {
    	var currentTime;
    	var currentHour;
    
    	// get the current date and time
    	currentTime = new Date();
    
    	// returns the hour in military time
    	currentHour = currentTime.getHours();
    	// check if the hour is between 6pm and 6am
    	if ((currentHour < 6) || (currentHour > 17)) {
    		window.location = nightURL;
    	} else {
    		window.location = dayURL;
    	}
    }
    // -->
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM>
    Click here to go to the next page:<BR>
    <INPUT TYPE="button" VALUE="Next"
    onClick="ampmRedirect('day.htm','night.htm')">
    </FORM>
    </BODY>
    </HTML>
    Last edited by jedaisoul; 07-16-2014 at 01:02 PM. Reason: code tags added

  2. #2
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    Code:
    function ampmRedirect(dayURL, nightURL) {
    var currentTime = new Date();
    var currentHour = currentTime.getHours();
    
      if ((currentHour < 6) || (currentHour > 17)) {
        window.location = nightURL;
      } else if (currentHour >=6 || currentHour <12) {
        window.location = dayURL;
      } else {
        window.location = otherURL;
      }
    }

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434

    Thumbs up Neat idea.

    TJ111:

    Your script triggered my imagination. Here's what I came up with.
    PHP Code:
    <html>
    <
    head>
    <
    title>Conditional Redirect</title>
    <
    script type="text/javascript">
    // uncomment 'window.location=...' commands when URLs are defined

    function ampmRedirect(nightURLdayURLotherURL) {
      var 
    currentTime = new Date();
      var 
    currentHour currentTime.getHours();
      if ((
    currentHour 6) || (currentHour 17)) {     alert(currentHour+' goes to: '+nightURL);
    //    window.location = nightURL;
      
    } else if (currentHour >=|| currentHour <12) {   alert(currentHour+' goes to: '+dayURL);
    //    window.location = dayURL;
      
    } else {                                              alert(currentHour+' goes to: '+otherURL);
    //    window.location = otherURL;
      
    }
    }

    var 
    RedirectURL = ['0.htm',  '1.htm''2.htm''3.htm''4.htm''5.htm''6.htm''7.htm',
                       
    '8.htm',  '9.htm','10.htm','11.htm','12.htm','13.htm','14.htm','15.htm',
                       
    '16.htm','17.htm','18.htm','19.htm','20.htm','21.htm','22.htm','23.htm'];
                       
    function 
    hourRedirect() {
      var 
    currentTime = new Date();
      var 
    currentHour currentTime.getHours();
      
    alert(currentHour+' goes to: '+RedirectURL[currentHour]);
    //    window.location = RedirectURL[currentHour];
    }
    function 
    randRedirect() {
      var 
    rnd Math.floor(Math.random()*24);
      
    alert(rnd+' goes to: '+RedirectURL[rnd]);
    //    window.location = RedirectURL[rnd];
    }
    </script>
    </head>
    <body>
    <button onClick="ampmRedirect('xxx','yyy','zzz')">Conditional Redirect</button>
    <button onClick="hourRedirect()">Hourly Redirect</button>
    <button onClick="randRedirect()">Random Redirect</button>
    </body>
    </html> 
    Could even expand it to redirects every minute or second if you go crazy.

    Thanks for the idea.

  4. #4
    Join Date
    Dec 2007
    Posts
    12

    still trouble

    I am still doing somthing wrong
    This is how i would like my redirects to function
    6am-9am=morning.html
    9am-5pm=day.html
    5pm-8pm=eve.html
    8pm-6am=night.html

    this is my code
    Code:
    <HTML>
    <HEAD>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function ampmRedirect(morningURL, dayURL, eveURL, nightURL) {
    var currentTime = new Date();
    var currentHour = currentTime.getHours();
    
      if ((currentHour < 6) || (currentHour > 20)) {
        window.location = nightURL;
      } else if (currentHour >=9 || currentHour <17) {
        window.location = dayURL;
      } else if (currentHour >=17 || currentHour <20) {
        window.location = eveURL;
      } else {
        window.location = morningURL;
      }
    }
    // -->
    </SCRIPT>
    </HEAD>
    <BODY onLoad="ampmRedirect('morning.html','day.html','eve.html','night.html')"">
    </BODY>
    </HTML>
    Any idea where i went wrong?
    Last edited by jedaisoul; 07-16-2014 at 01:03 PM. Reason: code tags added

  5. #5
    Join Date
    Dec 2007
    Posts
    12

    Thanks

    Also thanks for all your help guys and for your quick replies.

  6. #6
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    Sometimes Internet Explorer requires you use "window.location.href".

    Code:
    function ampmRedirect() {
      var currentTime = new Date();
      var currentHour = currentTime.getHours();
    
    
      if ((currentHour < 6) || (currentHour > 20)) {
        window.location.href = "night.html";
      } else if (currentHour >=9 || currentHour <17) {
        window.location.href = "day.html";
      } else if (currentHour >=17 || currentHour <20) {
        window.location.href = "eve.html";
      } else {
        window.location.href = "morning.html";
      }
    }

  7. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434

    Arrow Try this to see if it will work for you.

    Try this:
    PHP Code:
    var SpecialRedirectURL = ['night.html','morning.html','day.html','eve.html'];
                       
    function 
    specRedirect() {
      var 
    currentTime = new Date();
      var 
    currentHour currentTime.getHours();
      var 
    Hour 0;
      if (
    currentHour >= 6) { Hour 1; }
      if (
    currentHour >= 9) { Hour 2; }
      if (
    currentHour >= 17) { Hour 3; }
      if (
    currentHour >= 20) { Hour 0; }
      
      
    alert(Hour+' goes to: '+SpecialRedirectURL[Hour]);
    //    window.location.href = SpecialRedirectURL[Hour];

    Remove alert() when you are happy with results.

  8. #8
    Join Date
    Jul 2014
    Posts
    7
    Well that didn't work - when i have uploaded it to the site all it does it re load the website over and over again any idea guys here is the hidden link with it on so you can see what im dribbling on about

    http://www.sionedssizzler.co.uk/index1.html

  9. #9
    Join Date
    Jul 2014
    Posts
    7
    basically the shop opens at 8 in the morning and closes at 14.00

  10. #10
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434

    Question

    Quote Originally Posted by Netdaddy View Post
    Well that didn't work - when i have uploaded it to the site all it does it re load the website over and over again any idea guys here is the hidden link with it on so you can see what im dribbling on about

    http://www.sionedssizzler.co.uk/index1.html
    Well, that does not help much.

    On the link, what is it supposed to do?
    What display is supposed to be changed?

    Currently you have only two redirection locations:
    Code:
    var SpecialRedirectURL = ['closed.html','index1.html'];
    But the redirection is looking for 4 locations (0,1,2,3). Need to modify that function.

    Also, it appears that you NEVER call the function 'specRedirect()' for anything to occur.
    No redirect and no alert messages. You need to execute the function for it to do anything.

  11. #11
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434
    Here is some test code for the direction.
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> HTML5 page </title>
    
    </head>
    <body>
    <script type="text/javascript">
    // var SpecialRedirectURL = ['night.html','morning.html','day.html','eve.html'];
    var SpecialRedirectURL = [
       'http://www.sionedssizzler.co.uk/closed.html',
       'http://www.sionedssizzler.co.uk/index1.html'
    ];
                       
    function specRedirect() {
      var currentTime = new Date();
      var currentHour = currentTime.getHours();
      var Hour = 0;
      if ( (currentHour >= 8) && (currentHour < 14) ) { Hour = 1; }
    //  alert(Hour+' goes to: '+SpecialRedirectURL[Hour]);
      window.location.href = SpecialRedirectURL[Hour];
    }  
    window.onload = function() {
      specRedirect(); 
    }
    
    </script>
    
    </body>
    </html>
    Note, the time checks are for local time only.
    My open and closing hours are different from yours here in South Florida, USA.
    May not be a problem if your customers are only in the same time zone as you.

  12. #12
    Join Date
    Jul 2014
    Posts
    7
    Basically they are open between 8 am and 14.30 everything before and after them hours needs to re direct them to the closed.html if you get my drift in them hours it's just the normal index1 that needs to be loaded

  13. #13
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,500
    Code:
    switch( new Date().getHours() ){
    	// hours 06:00 to 08:59 
    	case 6: case 7: case 8: 
    		html = "morning.html";
    	break;
    	
    	// hours 09:00 to 16:59
    	case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16:  
    		html = "day.html";
    	break;
    	
    	// hours 17:00 to 19:59	
    	case 17: case 18: case 19: 
    		html = "evening.html";
    	break;
    
    	// hours 20:00 to 05:59	
    	default:
    		html = "night.html";
    
    }
    
    location.replace(html);
    Unfortunately you can not have exactly the time splits you want because if you did then your logic would be flawed.

    I question why you are doing this client side and not server side where your server time + offset for local time provides the correct page for your shop, you could have someone from a different timezone find your site, you are closed but the page directs the client to the page saying you are open.

    In PHP this would roughly be
    PHP Code:
    date_default_timezone_set('America/Los_Angeles');
    $time date("H");
    switch( 
    $time ){
        
    // hours 06:00 to 08:59 
        
    case 6: case 7: case 8
            
    $html "morning.html";
        break;
        
        
    // hours 09:00 to 16:59
        
    case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16:  
            
    $html "day.html";
        break;
        
        
    // hours 17:00 to 19:59    
        
    case 17: case 18: case 19
            
    $html "evening.html";
        break;

        
    // hours 20:00 to 05:59    
        
    default:
            
    $html "night.html";

    }

    header("Location: $html); 
    You would need to put in the timezone feature and any offset that exists between your local time and the server time. you will need to find your timezone identifier that your server will use.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  14. #14
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434
    Quote Originally Posted by Netdaddy View Post
    Basically they are open between 8 am and 14.30 everything before and after them hours needs to re direct them to the closed.html if you get my drift in them hours it's just the normal index1 that needs to be loaded
    Did you change the requirements between post #9 and #12?
    In the first case you wanted to stop taking orders at 14:00 and in the second you change to 14:30.
    So which is it?

    Also, did you try the code in post #11?
    What is the problem with that code to fit your needs?
    Is it only that it needs to be re-directed after 14:30 and before 8:00?

  15. #15
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434
    Quote Originally Posted by JMRKER View Post
    Did you change the requirements between post #9 and #12?
    In the first case you wanted to stop taking orders at 14:00 and in the second you change to 14:30.
    So which is it?

    Also, did you try the code in post #11?
    What is the problem with that code to fit your needs?
    Is it only that it needs to be re-directed after 14:30 and before 8:00?
    If it is to be 14:30 (military time) and only to be opened or closed consider this modification
    consider this modification. This would be your new index.html and to would redirect to the currently opened display filename.

    Server side solution would be better if your clients are in a different time zone,
    but then perhaps the food would be cold before you could deliver it anyway

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> HTML5 page </title>
    
    </head>
    <body>
    <script type="text/javascript">
    // var SpecialRedirectURL = ['night.html','morning.html','day.html','eve.html'];
    var SpecialRedirectURL = [
       'http://www.sionedssizzler.co.uk/closed.html',
       'http://www.sionedssizzler.co.uk/index1.html'
    ];
                       
    function specRedirect() {
      var currentTime = new Date();
      var currentHour = currentTime.getHours();
      if (currentHour < 10) { currentHour = '0'+currentHour; }
      var currentMins = currentTime.getMinutes();
      if (currentMins < 10) { currentMins = '0'+currentMins; }
    
      var now = currentHour+':'+currentMins;
      var Hour = 0;
      if ( (now >= '08:00') && (currentHour < '14:30') ) { Hour = 1; }
    //  alert(Hour+' goes to: '+SpecialRedirectURL[Hour]);
      window.location.href = SpecialRedirectURL[Hour];
    }  
    window.onload = function() {
      specRedirect(); 
    }
    
    </script>
    
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles