Forum Moderators: coopster

Message Too Old, No Replies

Need php code to display different pages based on time and day of week

         

mattb123

7:52 pm on Dec 29, 2015 (gmt 0)

10+ Year Member



Hi guys.
I'm pretty new to php but have used it a few times. I don't know where to start to get a php code for what I need and need some help.

What I want is to have 2 different index.html pages. let's say index.html and index1.html .
I want index.html to come up when we are open and index1.html to come up when we are closed. It seems that php would be best for this.

We are open:
mon-thur 7AM-7:30PM
Friday 7AM-6:30PM
Saturday 7AM-5:30PM

We are closed Sunday.

I need to get a php to send people to the 2 different html pages based on these times.

When this php is created, would that php file, index.html and index1.html all go in the root folder?
And then would I need to put any php code in the html itself?

Thanks!
Matt

lucy24

1:46 am on Dec 30, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



:: sparring for time until whitespace or someone like him shows up ::

And then would I need to put any php code in the html itself?

Only if you went with Option B: have a single page but generate it dynamically so it serves up different content based on server time. In that case you'd need to set an extremely short expiration time-- an hour or so-- so users don't see the "wrong" content if they check back 12 hours later. In fact, is that the reason you want two different pages, like an "open now" page and a "closed now" page, so browsers don't cache the wrong one? It wouldn't work, though, since browser caching is based on URL, not physical file. (Server caching/expiration is based on physical file.)

I assume you've already eliminated the javascript approach, which is probably the simplest of all but doesn't help with users who have scripting turned off. (So long as you remember to check something absolute, like server time or UTC, and not the user's local time!)

would that php file, index.html and index1.html all go in the root folder?

Technically they don't have to. A DirectoryIndex file doesn't absolutely have to live in the directory that it's acting as index for, though it's certainly less complicated that way. Similarly the php can be located anywhere that a request can "see" it.

I think before you attack the "how to" you need to be absolutely certain about what you want to achieve, and why.

Dammy

11:44 am on Dec 30, 2015 (gmt 0)

10+ Year Member



.

[edited by: Dammy at 11:47 am (utc) on Dec 30, 2015]

Dammy

11:44 am on Dec 30, 2015 (gmt 0)

10+ Year Member



You can create the two php pages ( e.g. index0.html & index1.html ) and computd your timing code on 'index.php/any other index.*' file e.x.
<?php
if($_Its_Sunday){ include('index0.html'); } else { include('index1.html'); } ... ?>

mattb123

8:58 pm on Dec 31, 2015 (gmt 0)

10+ Year Member



Thank for all of your responses. I have been piecing it together and came up with the following. It hasn't worked but it looks like it's close. Can someone tell me what's wrong?
I set it to go to test1.html when we are open and test2.html when we are closed. We are in open hours now but it goes to the closed page.
This code is in an index.php file in the root directory.


<?php


$day = date('N') ;
$time = date('H:i') ;

if (($day <= '4') && ($time >= '7:00') && ($time <= '19:30'))
{
echo readfile ("test1.html");
}

elseif (($day == '5') && ($time >= '7:00') && ($time <= '18:30'))
{
echo readfile ("test1.html");
}

elseif (($day == '6') && ($time >= '7:00') && ($time <= '17:30'))
{
echo readfile ("test1.html");
}



else
{
echo readfile ("test2.html");
}






?>

lucy24

11:05 pm on Dec 31, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



With all those ifs and elseifs, wouldn't it be cleaner to start by setting a variable-- say $open --to either true or false, and then do the readfile business based on this value? Now, me, I'd use a case/select/switch structure instead of an if/else/then, but that's probably more about personal coding style.

Dammy

12:05 am on Jan 1, 2016 (gmt 0)

10+ Year Member



Its true, but i think hes a beginner

whitespace

5:48 pm on Jan 1, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



As mentioned in your other thread [webmasterworld.com], your code sample uses lexicographical string comparisons, so you'll need a "0" prefix on your times. eg. "07:00", not "7:00".

Also make sure your server time is the correct timezone of your "shop". You should be explicitly setting this at the top of your script somewhere. For example:


date_default_timezone_set('Asia/Tokyo'); // Or whatever your timezone is (see reference below)!


Reference:
http://php.net/manual/en/function.date-default-timezone-set.php [php.net]

would that php file, index.html and index1.html all go in the root folder?


From your example "test1.html" and "test2.html" should ideally be stored outside of your public HTML folders. Since they are only being included by your server-side script they do not need to be publically available and run the risk of being picked and indexed by the search engines.