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:
    967
    Likes Received:
    455
    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:
    500
    Likes Received:
    257
    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 guys, you´re really great.
    thanks and +rep given...
    But because I don´t know anything about php I still need to have a closer look at it and search how exactly I can adopt it.
    And let´s say I want abc.php shown from monday to friday everyday from 7am to 6pm and xyz.php monday to friday from 6pm to 7am and from friday 6pm to monday 7am...
    Is something complicated like this also possible?
    I mean to integrate these different rules in one file?
    Or is it only possible to setup the rules for either daytime or weekday?
    Then I think it could be better to setup the rules for daytimes and then depending of the outcome redirect to another file with the rule for the days.
    Or other way round, first check the day and then the daytime...

    Well, once more I see I´m really not born as a coder...
     
    • 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,867
    Likes Received:
    1,335
    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,867
    Likes Received:
    1,335
    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
    Ok, but the abc.php and xyz.php will be other redirects anyway so it doesn´t matter which one I use right?
    Or would it be a disadvantage in some way when I would use
    Code:
    header("Location: xyz.php");
    ?
    Because I think for me it would be even better to use the redirect instead of the include because I assume with the include the URL the user would see would be www.mysite.com/abc.php then right?
    And that´s what I don´t want.
    I just want to redirect the visitors based on the timed rules.
     
  12. artizhay

    artizhay BANNED BANNED

    Joined:
    Nov 21, 2010
    Messages:
    1,867
    Likes Received:
    1,335
    Oh, yes, if those pages already redirect it would be best to use the header redirect.
     
  13. BlueZero

    BlueZero Power Member

    Joined:
    Jul 6, 2011
    Messages:
    500
    Likes Received:
    257
    Occupation:
    Webdeveloper, Project Manager
    Location:
    Byte in the Net
    Home Page:
    I am not sure i understand. But if you use include('abc.php') in index.php then user will see it on www.mysite.com/ not www.mysite.com/abc.php
     
    • 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.