1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP script to switch site content at different times possible?

Discussion in 'PHP & Perl' started by darkstar69, Feb 14, 2012.

  1. darkstar69

    darkstar69 Jr. VIP Jr. VIP

    Joined:
    Nov 15, 2008
    Messages:
    806
    Likes Received:
    880
    Occupation:
    Big Boss
    Location:
    On The Dark Side
    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
     
  2. cbnoob

    cbnoob Senior Member

    Joined:
    Sep 27, 2010
    Messages:
    969
    Likes Received:
    457
    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
     
    • Thanks Thanks x 1
  3. 45415

    45415 Junior Member

    Joined:
    Feb 5, 2009
    Messages:
    194
    Likes Received:
    92
    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'.
     
    • Thanks Thanks x 2
  4. BlueZero

    BlueZero Power Member

    Joined:
    Jul 6, 2011
    Messages:
    506
    Likes Received:
    261
    Occupation:
    Webdeveloper, Project Manager
    Location:
    Byte in the Net
    Home Page:
    Make a simple index.php

    PHP:
    $actualHour date('H');
    if ( 
    $actualHour  >=  && $actualHour ) include('nighttime.php');
    elseif ( 
    $actualHour  >=  && $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.
     
    • Thanks Thanks x 2
  5. darkstar69

    darkstar69 Jr. VIP Jr. VIP

    Joined:
    Nov 15, 2008
    Messages:
    806
    Likes Received:
    880
    Occupation:
    Big Boss
    Location:
    On The Dark Side
     
    • Thanks Thanks x 1
    Last edited: Feb 14, 2012
  6. nerdmoney

    nerdmoney Junior Member

    Joined:
    Feb 24, 2008
    Messages:
    135
    Likes Received:
    37
    Occupation:
    web nerd
    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
     
  7. darkstar69

    darkstar69 Jr. VIP Jr. VIP

    Joined:
    Nov 15, 2008
    Messages:
    806
    Likes Received:
    880
    Occupation:
    Big Boss
    Location:
    On The Dark Side
    Yes, I already checked it.
     
  8. artizhay

    artizhay BANNED BANNED

    Joined:
    Nov 21, 2010
    Messages:
    1,868
    Likes Received:
    1,336
    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.
     
    • Thanks Thanks x 1
    Last edited: Feb 14, 2012
  9. darkstar69

    darkstar69 Jr. VIP Jr. VIP

    Joined:
    Nov 15, 2008
    Messages:
    806
    Likes Received:
    880
    Occupation:
    Big Boss
    Location:
    On The Dark Side
    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");
    ?
     
  10. artizhay

    artizhay BANNED BANNED

    Joined:
    Nov 21, 2010
    Messages:
    1,868
    Likes Received:
    1,336
    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.
     
  11. darkstar69

    darkstar69 Jr. VIP Jr. VIP

    Joined:
    Nov 15, 2008
    Messages:
    806
    Likes Received:
    880
    Occupation:
    Big Boss
    Location:
    On The Dark Side
     
  12. artizhay

    artizhay BANNED BANNED

    Joined:
    Nov 21, 2010
    Messages:
    1,868
    Likes Received:
    1,336
     
  13. BlueZero

    BlueZero Power Member

    Joined:
    Jul 6, 2011
    Messages:
    506
    Likes Received:
    261
    Occupation:
    Webdeveloper, Project Manager
    Location:
    Byte in the Net
    Home Page:
     
    • Thanks Thanks x 1
  14. darkstar69

    darkstar69 Jr. VIP Jr. VIP

    Joined:
    Nov 15, 2008
    Messages:
    806
    Likes Received:
    880
    Occupation:
    Big Boss
    Location:
    On The Dark Side
    yes, sounds logical.
    but doesn't matter anyway because i think i will just use the redirect.
    thanks again to both of you.
    will try to implement the script into some campaigns tomorrow.