1. Home
  2. Computing & Technology
  3. PHP / MySQL

Redirect Based on the Day of the Week

By , About.com Guide

How do you redirect users to a different page based on the day of the week?:

Perhaps you run a restaurant and have long standing daily specials, like a Friday night fish fry. Or perhaps you have your own home page and like to read the news on week days but on Sundays you want to go straight to your favorite football team's website. You can setup a page to redirect based on the day of the week using the code below:
<?php $b = time () ;
$day = date("D",$b) ;

//Here we redirect the user to a differnt programming site based on what day of the week it is.

switch($day){
case "Sun": header( 'Location: http://php.about.com' ) ;
case "Mon": header( 'Location: http://webdesign.about.com' ) ;
case "Tue": header( 'Location: http://javascript.about.com' ) ;
case "Wed": header( 'Location: http://perl.about.com' ) ;
case "Thu": header( 'Location: http://python.about.com' ) ;
case "Fri": header( 'Location: http://ruby.about.com' ) ;
case "Sat": header( 'Location: http://cplus.about.com' ) ;
}

?>
The first thing this code does is find out what day of the week it is using the time () and date () functions. Next, we use a switch loop to perform different redirects based on which day of the week it is. Remember, like all PHP redirects, this needs to be the first thing on the page to work, before any other headers are sent.
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Step By Steps
  5. Redirect with PHP Based on the Day of the Week

©2009 About.com, a part of The New York Times Company.

All rights reserved.