www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 26 of 26

Thread: Redirect Based on Time of Day

  1. #16
    Join Date
    Jul 2014
    Posts
    7
    It's a local burger van that is going to be doing deliveries in a one mile radius so it is purely uk based he just wants the site to be re directed when the van is closed if you get my drift

  2. #17
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434
    Quote Originally Posted by Netdaddy View Post
    It's a local burger van that is going to be doing deliveries in a one mile radius so it is purely uk based he just wants the site to be re directed when the van is closed if you get my drift
    OK, so post #15 should do the trick.
    Requirements:
    1. Code of post #15 with a filename as 'index.html'
    2. Current file named 'index1.html' (which I would have named 'vanOpen.html' for clarity)
    and
    3. Current file named 'closed.html'. (which I would have named 'vanClosed.html' for clarity)

    All files should be in the same directory, unless further redirection is necessary.

  3. #18
    Join Date
    Mar 2012
    Posts
    1,814
    Quote Originally Posted by Netdaddy View Post
    It's a local burger van that is going to be doing deliveries in a one mile radius so it is purely uk based he just wants the site to be re directed when the van is closed if you get my drift
    What a hard way of doing something simple!!! He does not need redirects to achieve that. He just needs a simple bit of PHP to optionally display a div over the usual page saying something like "Sorry, we're closed. We will be open again..." depending upon the server time.

  4. #19
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,500
    IMHO this is better dealt with at the server, use a PHP script to return a "burger van status" value to a web page that uses an AJAX call to that script that doesn't redirect but displays if the "van" is open for business or is closed.

    Then a "real time" element can be added with a setInterval() call to an update script that checks every minute for the vans status.

    The problem with using JavaScript itself is that it relies on the clock on the PC to be set to the proper time and if the service is UK based then it won't matter where in the world you are, the server will only serve up "local time" and for those of us north of Watford gap, we will know if your mate is still open for business or not seeing as we are on a different continent and time zone :P
    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?

  5. #20
    Join Date
    Jul 2014
    Posts
    7
    uploaded it and it does not work its re directing on its self again was i meant to change anything ??? before uploading

    <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>

    This has been put on the index1 page

  6. #21
    Join Date
    Jul 2014
    Posts
    7
    apologies stupid machine it does work it re directs to the closed message excuse me for being a noob when it comes to scripting lol

    at the mo its re directing to the closed message when it should be open but according to if ( (now >= '08:00') && (currentHour < '14:30') ) { Hour = 1; } it should be open but its not

  7. #22
    Join Date
    Jul 2014
    Posts
    7
    Your option works mate cheers just how do i make it uk based the script actually works what bit of the script do i need to change to make it work for my hours

    Quote Originally Posted by JMRKER View Post
    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.

  8. #23
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    I agree with those who said that this should be dealt on server side. You can't rely on client's computer to get the correct time. Even if he/she is in the same time zone as you are it doesn't mean that his/her's system clock is set up correctly. The last thing you want to happen is to display to someone you're closed while your open and vice versa.

  9. #24
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434
    Quote Originally Posted by Netdaddy View Post
    Your option works mate cheers just how do i make it uk based the script actually works what bit of the script do i need to change to make it work for my hours
    There is no way for JS to compensate for the user who does not know how to set their PC clock.
    You would probably loose them as customers anyway because they would not know the difference between AM and PM.
    Hopefully, that particular population will be low.

    You can personally test your code at any time by TEMPORARILY changing your PC clock and running the program.
    It should display the correct opened or closed message depending on how your clock is set.
    Remember to set the time correctly when you are done.

    Assuming you have a user with their clock set correctly, you could still use getGMT... commands (google search utcDate()) and the time zone offset.
    But again, you are still at the mercy of the user's PC clock.

    This is why the recommendation to use your server clock or some site that references a known time.

    Note also that you could avoid the re-direction entirely by checking the time to show an <div id="openedVan"> OR an <div id="closedVan"> element.
    The proper <div> could have the display set to 'none' or 'block' depending upon the time of the PC clock.
    Same problem as before, but easier to maintain one file than it would be to maintain 3 (index.html, opened.html, closed.html) files.

    Good Luck!

  10. #25
    Join Date
    Mar 2012
    Posts
    1,814
    This is really easy to do in PHP. All you need is a DIV with position absolute that covers the input area of the burger bar order form but is hidden during opening hours. E.g.:

    CSS:
    Code:
    #closed {
    position:absolute;
    <?php if((date('H:i')>'07:59') && (date('H:i')<'14:30')) { ?>
    display:none;
    <?php ;} ?>
    }
    Here is a working test example:
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Burger Bar - Opening Time Test</title>
    <style>
    * {
    margin:0;
    padding:0;
    border:none;
    }
    html {
    width:100%;
    height:100%;
    background-color:#AAA;
    }
    body {
    position:relative;
    max-width:320px;
    max-height:320px;
    width:100%;
    height:100%;
    margin:0 auto;
    }
    #header {
    position:absolute;
    width:300px;
    height:40px;	
    padding:0 10px;
    left:0;
    top:0;
    color:#FFF;
    background-color:#077;
    }
    #subpage,
    #closed {
    position:absolute;
    width:300px;
    height:260px;	
    padding:10px 10px;
    left:0;
    top:40px;
    }
    #subpage {
    position:absolute;
    color:#FFF;
    background-color:#070;
    }
    #closed {
    position:absolute;
    color:#FFF;
    background-color:#700;
    text-align:center;
    /*** test for time ***/
    <?php if((date('H:i')>'07:59') && (date('H:i')<'14:30')) { ?>
    display:none;
    <?php ;} ?>
    /*** ***/
    }
    </style>
    </head><body>
    <div id="header">
    <h1>Opening Time <font size="-1">Test</font></h1>
    </div>
    <div id="subpage">
    <form action="./" method=post>
    Item <input type=text name="item1" size="10" value="Cheese Burger">
    No. <input type=text name="count1" size="2" maxlength="3" value="2"><br>
    Item <input type=text name="item2" size="10" value="Fries">
    No. <input type=text name="count2" size="2" maxlength="3" value="2"><br>
    Item <input type=text name="item3" size="10" value="Coke">
    No. <input type=text name="count3" size="2" maxlength="3" value="1"><br>
    Item <input type=text name="item4" size="10" value="">
    No. <input type=text name="count4" size="2" maxlength="3" value=""><br>
    <input type=submit name="submit" value="submit">
    </form>
    </div>
    <div id="closed">
    <br><Br><br><Br>
    Sorry we are closed.<br>
    We will be open at 8am.
    </div>
    </body>
    </html>
    Last edited by jedaisoul; 07-17-2014 at 03:33 PM.

  11. #26
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,500
    Think you may have a typo
    PHP Code:
    #closed {
    position:absolute;
    <?php if((date('H:i')>'07:59') && (date('H:i')<'14:30')) { 
    echo 
    "display:none;";
     } 
    ?>
    }
    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?

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