...

View Full Version : "Opening Hour" of a page



ickwong
05-03-2006, 12:22 PM
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.

Bill Posters
05-03-2006, 12:41 PM
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…

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

ickwong
05-04-2006, 04:24 AM
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!

Bill Posters
05-04-2006, 09:27 AM
e.g.

You'd insert this piece of PHP where you want the link to go…

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

ickwong
05-04-2006, 09:55 AM
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.

Bill Posters
05-04-2006, 11:01 AM
PHP: date() function (http://uk2.php.net/manual/en/function.date.php)

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


And do I just put that code where the link is?

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…

ickwong
05-04-2006, 12:12 PM
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!

mio
05-04-2006, 02:37 PM
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.

ickwong
05-08-2006, 08:42 AM
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)

Bill Posters
05-08-2006, 10:32 AM
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 (http://uk2.php.net/manual/en/language.operators.logical.php).

ickwong
05-12-2006, 03:59 AM
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???


<?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>';
?>

lavinpj1
05-12-2006, 08:30 AM
<?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>');
}
?>

Bill Posters
05-12-2006, 08:42 AM
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.

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

- - -



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

ickwong
05-12-2006, 09:03 AM
Thanks very much again guys. Glad problem is finally solved.

lavinpj1
05-12-2006, 09:11 AM
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~

Bill Posters
05-12-2006, 09:21 AM
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~
Yes, we could throw </p></body></html> into the die statement, but what about any actual content which followed that PHP passage?
Or are you suggesting that we throw the rest of the markup into the die statement as well? (I presume not as it would be highly impractical.)
If the page appeared near the top of the markup, the rest of the page wouldn't appear using your method.
What the OP wanted was a conditional link.

The other reason your code didn't produce the right outcome was because it was supposed to echo out the link during open hours and a 'closed' statement when closed. Your method did both at the same time, providing users with a link during closed hours which the author didn't want them to use.

lavinpj1
05-12-2006, 09:46 AM
Ah, I see. I misinterpretted the request. I thought the idea was...

check to see if right time, if not display a message and a link.

if it is right time, display rest of page instead.

~Phil~



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum