Load page based on day and time

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stuffdone
Forum Newbie
Posts: 3
Joined: Thu Jun 05, 2008 5:51 pm
Location: NE Florida

Load page based on day and time

Post by stuffdone »

I need a method to have a different page ( index.php / index2.php ) load based on day of week and time of day.

Anyone know a way to do this?

Reason: pizza order site needs to turn off order pages when store is closed.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Load page based on day and time

Post by califdon »

The first suggestion that comes to mind is to have a redirect page that is the one that is called by the browser, which just checks the time and redirects to one of two pages. You could even do it in the same script, of course, except that it might require a rather large file, depending on how much content you'd have in each of the pages.
helraizer
Forum Commoner
Posts: 31
Joined: Thu Jun 05, 2008 8:20 pm

Re: Load page based on day and time

Post by helraizer »

Code: Select all

 
<?php
 
$now = date("H:i:s"); // will display the hour, minute and seconds of the current time.
 
 
if($now <= "12:00:00") { //time is before 12 noon
    $orders = 0;
} else { // time is after 12 noon
    $orders = 1;
}
 
?>
 
Have a play with that. Say your pizza shop stops taking orders at 9pm then change <= "12:00:00" to >= "21:00:00" ($orders = 0) then orders can be taken up to 9pm.

You may also want to use && in there so $now >= "12:00:00" && $now <= "21:00:00" so orders can only be placed between 12-noon and 9pm.

Hope that helps.
Sam
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Load page based on day and time

Post by RobertGonzalez »

What time frames are you looking to switch around?
stuffdone
Forum Newbie
Posts: 3
Joined: Thu Jun 05, 2008 5:51 pm
Location: NE Florida

Re: Load page based on day and time

Post by stuffdone »

For example.

They are closed all day monday.
Several days of the week they open at 11AM closing at 10PM ( stop taking orders at 9:30 )
Fridays and Saturdays they open at 11AM and close at 1AM ( stop taking orders at 12:30AM )

I don't have the exact schedule in front of me but you get the idea !

I am not a programmer myself so am hoping someone has already figured this out. Perhaps some code I can place in the default index.php that will immediately redirect to the "closed.php" at the appropriate day and time.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Load page based on day and time

Post by RobertGonzalez »

Many developers have figured this out. A lot of use this type of code in our apps. I use this same scenario on our corporate intranet for placing announcements.

Of course, that code is mine and since this is a teaching forum chances are good no one here will just hand over code to you. But we would love to work with you on coding it yourself.
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: Load page based on day and time

Post by dbemowsk »

Since you are working with different times of the day based on the day of the week, you can look into the php date function. What I have done for things like this is use parts of the date function to generate a number that is easy to compare.

For example:
w - a numeric representation of the day of the week, 0=sunday to 6=saturday
H - 24 hour format with leading zeros
i - minutes with leading zeros

So, using the date function like

Code: Select all

date("wHi")
will give you a number like 51113 which is friday (5) at 11:13 AM, and 02130 would be sunday (0) at 21:30 or 9:30 PM.

Using this format you can create an array that has your open and close times for each day of the week like this:

Code: Select all

 
<?php
/*
array shown below is 
sunday - 01100...
monday - 11100...
............
friday - 51100...
saturday - 61100...
*/
$schedule = array(array("01100", "02130"),
                  array("11100", "12130"),
                  ...........................,
                  array("51100", "60030"),
                  array("61100", "62330"));
$takeOrder = false;
foreach ($schedule as $openClose) {
  if (time() >= $openClose[0] && time() <= $openClose[1]) {
    $takeOrder = true;
  }
}
if ($takeOrder) {
  include( 'order_page.php' );
} else {
  include( 'no_orders_page.php' );
}
?>
 
The code I have included is kind of crude, but it should give you the idea of what to do.

Hope that helps a bit.
stuffdone
Forum Newbie
Posts: 3
Joined: Thu Jun 05, 2008 5:51 pm
Location: NE Florida

Re: Load page based on day and time

Post by stuffdone »

dbemowsk wrote:Since you are working with different times of the day based on the day of the week, you can look into the php date function. What I have done for things like this is use parts of the date function to generate a number that is easy to compare.

For example:
w - a numeric representation of the day of the week, 0=sunday to 6=saturday
H - 24 hour format with leading zeros
i - minutes with leading zeros

So, using the date function like

Code: Select all

date("wHi")
will give you a number like 51113 which is friday (5) at 11:13 AM, and 02130 would be sunday (0) at 21:30 or 9:30 PM.

Using this format you can create an array that has your open and close times for each day of the week like this:

Code: Select all

 
<?php
/*
array shown below is 
sunday - 01100...
monday - 11100...
............
friday - 51100...
saturday - 61100...
*/
$schedule = array(array("01100", "02130"),
                  array("11100", "12130"),
                  ...........................,
                  array("51100", "60030"),
                  array("61100", "62330"));
$takeOrder = false;
foreach ($schedule as $openClose) {
  if (time() >= $openClose[0] && time() <= $openClose[1]) {
    $takeOrder = true;
  }
}
if ($takeOrder) {
  include( 'order_page.php' );
} else {
  include( 'no_orders_page.php' );
}
?>
 
The code I have included is kind of crude, but it should give you the idea of what to do.

Hope that helps a bit.
I will try to muddle through and give it a try. Is there an easier way, say rather than choosing pages like this, a conditional redirect so the default page for orders always comes up but is redirected to another page if it does not meet the date conditions?

The problem is the index.php which will always load by default IS the order page. Because the cart will always refer to this as the default in scripts etc, the most I would want to do is include something to switch at times closed.

It is not a matter of loading a second page from there but of loading an alternate page only if they are closed.

Pardon my dumbness on this but I don't know php other than to muddle around to make little tweaks.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Load page based on day and time

Post by califdon »

No need for an apology. Much as we hate to admit it, the fate of the world doesn't depend on everyone being a programmer. The problem is that these particular forums were established for the purpose of enabling developers to help each other solve problems, so we tend to spend our efforts on answering specific programming or structural questions for other developers, not writing or rewriting scripts for non-programmers.

That said, it shouldn't be very difficult to do what you need to do. Were it me, I'd probably use a short index.php script to redirect to one of 2 scripts (one them being the current index.php, renamed). The trick is how to determine the appropriate page, from an array of days/times, which is what dbemowsk was addressing. The redirection code script (the new index.php) should look something like this (I hardly ever do redirection, so someone may correct me):

Code: Select all

<?php
/*
  Set up days/times of operations array,
  Test against current day/time,
  Assign value of $open to either True or False
*/
if ($open) {
    header('Location: http://example.com/openindex.php');
    // the above would be your old "index.php" script
} else {
    header('Location: http://example.com/closedindex.html');
    // the above would be your new page to display when the store is closed
}
?>
Post Reply