PHP script to switch site content at different times possible?

darkstar69

Senior Member
Joined
Nov 15, 2008
Messages
803
Reaction score
886
Hey there.
Is it possible to have a php script that switches the site content at different times of a day or on different weekdays?
For example:

You got www.mysite.com

From monday to friday you get redirected to www.mysite.com/weekdays.php

and on saturday and sunday you get redirected to www.mysite.com/weekend.php

And still better would be if it also could work with different daytimes.

I hope it makes sense and you understand what I mean.

Thanks in advance
 
yeah, it is possible. Here is the basic.
you get, $currenttime
if $currenttime = x {echo 'page content for that specific day here';}
then, you don't need to change url, just content of the index.php file
 
Something like this should work

PHP:
<?php
$getdate = getdate();
if($getdate['wday'] <= 5) {
  header('Location: http://www.example.com/weekdays');
}
else {
  header('Location: http://www.example.com/weekend');
}
?>
For the daytime check you do the same, just replace 'wday' with 'hours'.
 
Make a simple index.php

PHP:
$actualHour = date('H');
if ( $actualHour  >=  0 && $actualHour < 8 ) include('nighttime.php');
elseif ( $actualHour  >=  8 && $actualHour < 12 ) include('beforelunch.php');
elseif ( $actualHour  >=  12 && $actualHour < 18 ) include('daytime.php');
elseif ( $actualHour  >=  18 && $actualHour < 24 ) include('evening.php');

That would do it.
 
just dont forget these all call server time, so if you are in pacific time zone, but your server is GMT, then it needs to be adjusted the nine hours or whatever to account for that
 
just dont forget these all call server time, so if you are in pacific time zone, but your server is GMT, then it needs to be adjusted the nine hours or whatever to account for that

Yes, I already checked it.
 
PHP:
// get day in numeric form (1-7, Mon-Fri); get hour in 24-hour (0-23)
list($dateDay, $dateHour) = explode(",", date("N,G", time()));

// determine if day is from Monday to Friday
if ($dateDay <= 5) {
   // determine if it's between 7am and 6pm
   if (($dateHour >= 7) && ($dateHour <= 18)) {
      include_once("abc.php");

   // otherwise it's 6pm to 7am
   } else {
      include_once("xyz.php");
   }

// otherwise, it's the weekend
} else {
   include_once("xyz.php");
}

The code is unnecessarily long for your specific requirements, but it allows you to adapt it to different future needs.
 
Last edited:
PHP:
// get day in numeric form (1-7, Mon-Fri); get hour in 24-hour (0-23)
list($dateDay, $dateHour) = explode(",", date("N,G", time()));

// determine if day is from Monday to Friday
if ($dateDay <= 5) {
   // determine if it's between 7am and 6pm
   if (($dateHour >= 7) && ($dateHour <= 18)) {
      include_once("abc.php");

   // otherwise it's 6pm to 7am
   } else {
      include_once("xyz.php");
   }

// otherwise, it's the weekend
} else {
   include_once("xyz.php");
}

The code is unnecessarily long for your specific requirements, but it allows you to adapt it to different future needs.

thank you very much.
this looks very good and i think even i can understand it :D
but just out of curiosity, can you tell me why the
Code:
include_once("xyz.php");
is better then
Code:
header("Location: xyz.php");
?
 
thank you very much.
this looks very good and i think even i can understand it :D
but just out of curiosity, can you tell me why the
Code:
include_once("xyz.php");
is better then
Code:
header("Location: xyz.php");
?
Awesome, I hope it works for you!

I originally had it as a redirect, but I think an include is better for general use. For example, people tend to not trust redirects, so loading it to the current file may be best. Or if you're promoting on AdWords for example, redirects are kind of iffy depending on the source/destination, so best to stay on one page. Or if you're doing SEO, it may be bad to SERP placement if you keep redirecting to different pages all the time. Plus the separate files will be indexed and accessible.

Really just whatever is best for your use.
 
Back
Top