Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    "Opening Hour" of a page

    Hi, I have a question would like to ask if you guys expert can give me some help please.

    Basically, I have a link here in a page "A" which links to another page "B". Now I would like to set something similar to an opening hour restriction on the page "B", which for example the user can only enter page "B", say, within 9am to 9pm. When for example the user clicks on the link (in page "A") at 7am, then it will not allow the user to enter page "B", and instead will take you to another page "C", in which it says "please come back between 9am to 9pm".

    Now my question is, is this do-able? And if so, how should I go about writing the code? (I'm a PHP/Javascript beginner by the way...)

    Thanks very much in advance.

  • #2
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Assuming that the hour you wish to reference is that of your host server (which, presumably, is in the right timezone for your needs)…

    Place this PHP as the very first piece of code in the "B" document…
    Code:
    <?php
    
    	if (date("G") < 9 || date("G") > 21) header("Location: ./closed.php");
    
    ?>
    There are other ways this could be done.
    e.g. You could actually set the href of the link on page "A" according to the time, so the single link always points to the appropriate page.
    Alternatively, you could simply replace the link on page "A" with a 'closed' notice during non-open hours.
    Last edited by Bill Posters; 05-03-2006 at 12:43 PM.

  • #3
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply Bill Posters. Can I just ask, if I use the alternative method to "set the href of the link on document 'A' according to the time", how should I do it? Since I don't kind of have access to document 'B'......
    Thanks!
    Last edited by ickwong; 05-04-2006 at 04:29 AM.

  • #4
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    e.g.

    You'd insert this piece of PHP where you want the link to go…
    Code:
    <p><?php
    	echo (date("G") < 9 || date("G") > 21) ? 'Sorry, we are currently closed.' : '<a href="b.php">Enter the shop</a>';
    ?></p>
    The above process uses a 'ternary conditional' which is just another way of presenting if/else conditional statements.

  • #5
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just to make sure I understand the code rather than just copy & paste it, what is that "G" in the code? And do I just put that code where the link is?
    Thank you again for your reply.

  • #6
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    PHP: date() function

    G - 24-hour format of an hour without leading zeros (0-23)

    Quote Originally Posted by ickwong
    And do I just put that code where the link is?
    Quote Originally Posted by Bill Posters
    You'd insert this piece of PHP where you want the link to go…
    You might need to remove the <p>…</p> tags from my code, depending on your surrounding code.
    Make sure the final output (in the source) is correctly nested, etc…
    Last edited by Bill Posters; 05-04-2006 at 11:06 AM.

  • #7
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Bill Posters, sorry to bother again. I've just tried the code, but it still takes me to document 'B' successfully without displaying the "closed" message. (I've already modified the range of the time by the way) Any ideas why that is???
    Thanks!

  • #8
    mio
    mio is offline
    New Coder
    Join Date
    May 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Check the server date/time, this could be different from the client so you don't get the result you expected i.e. you set 8am to 10am page locked, it's 12pm server side but client time is 9am.

  • #9
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks guys! After a couple minor modifications, it is finally working now.
    However, one more question would like to ask, is it possible to set a restriction of day on this piece of code as well? (eg. Monday to Friday, 9am-5pm)

  • #10
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Quote Originally Posted by ickwong
    is it possible to set a restriction of day on this piece of code as well? (eg. Monday to Friday, 9am-5pm)
    Take another look at the PHP manual's date() function page.
    You should be able to work it out for yourself from the values available how to implement additional checking based on the day of the week.
    Tip: look for a format character which gives you a numeric representation of the day of the week.

    Combine checks for day of the week and hour of the day using logical operators.

  • #11
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi, I have managed to modify your code and added a condition for the day as well. But I'm not sure why it's not working. Any ideas what's wrong???

    Code:
    <?php
       echo ((date("w") < 1 || date("w") > 5) && (date("G") < 9 || date("G") > 17)) ? 'Sorry, this link is not available now!' : '<a href="http://abcd.php">Link</a>';
    ?>

  • #12
    Regular Coder
    Join Date
    Sep 2005
    Posts
    394
    Thanks
    1
    Thanked 0 Times in 0 Posts
    <?php
    if ((date("w") < 1) || (date("w") > 5) || (date("G") < 9) || (date("G") > 17)) {
    die('Sorry, this link is not available now!<p> Please use this <a href="http://abcd.php">Link</a>');
    }
    ?>
    Last edited by lavinpj1; 05-12-2006 at 08:34 AM.

  • #13
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Close.

    Getting the checks right can sometimes be tricky, so you need to think through it logically.

    Swap the && (and) for an || (or)...

    i.e.
    Code:
    echo ((date("w") < 1 || date("w") > 5) || (date("G") < 9 || date("G") > 17)) ? 'Sorry, this link is not available now!' : '<a href="http://abcd.php">Link</a>';
    - - -

    Quote Originally Posted by lavinpj1
    Code:
    <?php
    if ((date("w") < 1) || (date("w") > 5) || (date("G") < 9) || (date("G") > 17)) {
    die('Sorry, this link is not available now!<p> Please use this <a href="http://abcd.php">Link</a>');
    }
    ?>
    That won't produce the right outcome.

    We certainly won't want to be using die() as it kills the rest of the page's markup (including any content which follows), leaving the page malformed, invalid and incomplete.

  • #14
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks very much again guys. Glad problem is finally solved.

  • #15
    Regular Coder
    Join Date
    Sep 2005
    Posts
    394
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Other than the die thing, I don't see why it would not produce the right output. And the die would merely miss off a section of close tags at the bottom which could be included in the die statement anyway.

    ~Phil~


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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