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

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?


I would think the end result would be a single index file like “index.php”.
But the html that that shows the visitor would vary depending upon the time.

It’s usually the other way around. You have html in the php. The php checks the time to determine if you are open, then writes the appropriate html.

if ( $open == true ) { echo "This html." ; }
else { echo "That html." ; }

Or you could have the two html files as includes:

if ( $open == true ) { include "includes/open.html" ; }
else {  include "includes/closed.html" ; }

There is always a number of ways with php.

Personally I would go for the include method as your echo html could be quite a lot of info.


Presumably those are times in your local timezone. Is the server in your timezone? If not then you need to take that into account in setting up the test for whether you are open or not.

Rubble. Would having a lot of html data in the php cause any problems? Would it slow down the opening of the site, for instance?

I am not sure about the speed but having an include would make it easier to maintain.
You can have to separate include files with all your information and if you wanted to change something in the day file you would go to that file. With all the code in the php echo you would have to search through the code and check if it was day or night.
I suppose it comes down to what is in your two different pages and how often you might want to change it.

It is much easier to write html in html. In an echo statement, you have to escape quotes and things like that.
The best way really depends on the page structure and just how different the two will be.
If the two are mostly the same, and a small part is different, like “We are Open!” then just echo it, but if the whole thing is different, use an include.
If there is some the same in both, like the header and footer, that could all be in the “index.php” as plain html (not echoed) and just the main content in the middle could be the html include.
There are so many different ways, depending on your specific needs.

I’m based in the UK and have a site about my locality, but the host server is in the USA, so the default timezone is not local. But I set the timezone to Europe/London in the php.ini file.

This should work, with the correct timezone set.

// get the time and day
 $day = date('N') ; // Day of the week; 1 = Monday, 7 = Sunday
 $time = date('H:i') ; // The time, HH:MM
 if ( $time > '07:00') { // it is after 7am
  if (( $day < 5 ) && ( $time < '19:30')) { $open = true ; }  // it is monday to thursday and before 7:30pm
  elseif (( $day == 5 ) && ( $time < '18:30')) { $open = true ; }  // it is friday and before 6:30pm
  elseif (( $day == 6 ) && ( $time < '17:30')) { $open = true ; }  // it is saturday and before 5:30pm
 else { $open = false ; } // none of the above
 // Display conditions
 if ( $open == true ) {
  echo "<h1>We are Open!</h1>" ; // or more likely an include
 else {
  echo "<h1>We are Closed!</h1>" ;

I tried that code but it didn’t work. I may be doing something wrong with it.

I created a php file that has your code in it. In my index.php, I have an include that points to that file. But my index.php just displays all the code. Was that the final version that I should be able to plug into a blank php file? Or is there other code at the start or finish that I need to put in there for it to work (such as php starting or closing tag. )

What you put together definitely looks like what I’m looking for.

Yes you need to enclose the script in php tags? As in

<?php    // to open

The script

?>   // and to close

Also check you do have php installed on the server. Though if you did not use the tags, that is probably it. This is a test script:

    $style = "#nophp {display: none;}";
    $mystring="I have PHP!";
    <title>PHP Test</title>
        body    {text-align: center;}
        <?php echo $style;?>

        <h1>PHP Test</h1>
        <?php echo $mystring;?>
        <p id="nophp">You do not have PHP!</p>
        <p>If you have PHP, it will say so.</p>

On the other hand, maybe my script does not work, I’m not the best at php. I did test it, but not thoroughly. The bit I had doubts about was using the time in that format, but it did appear to work in tests.
If there is something very wrong, I’m sure someone here more able will spot it.
The one thing that is wrong with it, but not disastrous, is the else clause, commented “none of the above” that only applies to not being before 7am, ignoring the other conditions. But that line is not really needed anyway. You could remove it and if you like put $open = false; at the very start to be sure of a reset.

The only obvious thing I see with the test example code is the opening html tag should be before the opening head tag.

1 Like

Well spotted.
How about the time check script, any improvements?

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"); } ?>

You are missing the leading zeros on the 07:00s.
It is a little more long-winded than my earlier example. This is an improved and more condensed version:

 $day = date('N') ;
 $time = date('H:i') ;
 if ( $time > '06:59') {
  if (( $day < 5 ) && ( $time < '19:30')) { $open = true ; }
  elseif (( $day == 5 ) && ( $time < '18:30')) { $open = true ; }
  elseif (( $day == 6 ) && ( $time < '17:30')) { $open = true ; }
 if ((isset($open)) && ( $open == true )) { include "test1.html" ; }
 else { include "test2.html" ; }

Hi SamA.

I tried that code you posted but it didn’t work. It still shows the wrong page.

I spotted that I was missing the leading 0s. I fixed that but it still wasn’t working.

One part I don’t understand is the $open = true. I don’t see where $open is defined as a variable.

I’d suggest placing the open/closed test in a function - that way the main code is more readable and more flexible - you just return $open from that function to use in the rest of the code.

Also ( $open == true ) is pointless - you should either use ( $open= == true ) or ($open) for the test. The first of these makes sure that the value is actually true and not just any non-falsy value while the second is a shorter equivalent to what you have.

You can also avoid needing to test of $open isset by simply setting it to false before the if statements.

Can you really compare strings like that? I’ve never tried it.

You are comparing those times as though they are integers (less than and greater than) even though they are strings - “7:00” is a string. If you simply make your script check $date and not $time, it works fine.

Can you think of a better way to compare times to meet your goal here?

I was thinking the same thing.

“Human friendly” time strings are great for us to read, machines not so much.

I know using < = > on strings can cause unexpected results. The characters are evaluated as binary values.

I didn’t want to test at different times so I created an array and a callback function

 $str_time_arr = array( '00:01' 
			, '11:35'
			, '22:31'
			, '23:59'
			, '00:59'
			, '14:40'
			, '15:07'
			, '12:23'
			, '13:43'
			, '5:51'
			, '06:15'
			, '18:35'
			, '19:42'
			, '20:49'
			, '21:56'
			, '2:05'
			, '03:55'
			, '16:21'
			, '17:28'
			, '04:09'
			, '01:00'
			, '07:45'
			, '08:22'
			, '09:42'
			, '10:25'
function compare_time_strings($a, $b) {
  if ($a == $b) {
    return 0;
  return ($a < $b) ? -1 : 1;
usort( $str_time_arr, "compare_time_strings");
//sort( $str_time_arr, SORT_STRING);
echo "<pre>callback sort "; 
echo  "</pre>";

when some are missing the leading zero the sort fails

callback sort array(25) { [0]=> string(5) "00:01" [1]=> string(5) "00:59" [2]=> string(5) "01:00" [3]=> string(5) "03:55" [4]=> string(5) "04:09" [5]=> string(5) "06:15" [6]=> string(5) "07:45" [7]=> string(5) "08:22" [8]=> string(5) "09:42" [9]=> string(5) "10:25" [10]=> string(5) "11:35" [11]=> string(5) "12:23" [12]=> string(5) "13:43" [13]=> string(5) "14:40" [14]=> string(5) "15:07" [15]=> string(5) "16:21" [16]=> string(5) "17:28" [17]=> string(5) "18:35" [18]=> string(5) "19:42" [19]=> string(5) "20:49" [20]=> string(5) "21:56" [21]=> string(5) "22:31" [22]=> string(5) "23:59" [23]=> string(4) "2:05" [24]=> string(4) "5:51" }

but when they all have leading zeroes it sorted OK

It feels chancy, but I guess it works.

More verbose would be to put the time values to Int with the minutes as fractions of an hour and compare as Floats

echo "<pre>"; 
$rd = date('Y-m-d H:i:s');
$dp = date_parse($rd);
 echo  "</pre>";

array(12) {



As I mentioned I was not sure, since they are strings, not a time or integer, but it seems to work.

I did think at first I would need to use some kind of integer time format, but when the strings appeared to work I thought that would be easier to understand and edit if needed, from a human point of view.

That line is $open being defined as a variable.

I don’t know why, I have it working here. I did want some of the more experience programmers to look over it and possibly improve, and it seems they have now. If there were something glaringly wrong, I’m sure it would be spotted. The suggestions given seem more like enhancements, rather than fixing what is broken.