PHP - Content depending on day of week

8 replies
Hey,

I want to load a different bit of content depending on the day of the week.

It's working.. I think..

I have this in the <head>..

Code:
<?php

$day=date("w");
$file="t".$day.".php";

// anywhere on your page, you include the file.
//include($file);

?>
.. and then, of course, I have..

<?php include($file);?>
.. where I want the daily content to be ..

and then in the same directory I have..

t0.php (Sunday)
t1.php (Monday)
t2.php (Tuesday)
t3.php (Wednesday)
t4.php (Thursday)
t5.php (Friday)
t6.php (Saturday)

What I'm wondering is.. will the content depend on my server or will it depend on the users timezone...

Obviously I want it based on my users timezone..

Could someone please advise..
#content #day #depending #php #week
  • Profile picture of the author RenardNET
    This code works only based on server time because PHP is server side language. If you want to have different content based on user time you will need solution working on user side (pure JS or JQuery) in browser after page was loaded which will read user time and load new content into page by AJAX.

    Tom
    {{ DiscussionBoard.errors[8089325].message }}
  • Profile picture of the author Joe Motion
    Hey Tom,

    I had a feeling that was the case.

    What do you recommend.. Javascript?

    I'll pay for a fully working solution!!
    Signature
    Living in SE Asia.. BKK.. PM me for a beer!
    {{ DiscussionBoard.errors[8089327].message }}
  • Profile picture of the author RenardNET
    Pure JS is harder to use. So it is always better to have such function wrote int JQuery or Prototype (or otehr) because they offer easy AJAX helper functions. So if you already use JQuery or Prototype libraries in your website than better is to use them. If you have WordPress then probably you use JQuery too in your theme.

    I don't have time to write solution for you but this is nothing very hard so I am sure than you will have here many offers from other warriors if you want to pay for that.

    Tom
    {{ DiscussionBoard.errors[8089337].message }}
  • Profile picture of the author Joe Motion
    Thanks Tom.

    If anyone has a 100% working solution that will display code based on the day of the week on that persons browser please PM me with a price.

    J
    Signature
    Living in SE Asia.. BKK.. PM me for a beer!
    {{ DiscussionBoard.errors[8089344].message }}
    • Profile picture of the author Brandon Tanner
      This is just off the top of my head, so you'll need to test it to make sure it works. It uses Javascript to get the current day of the week, so it will be based on where your website visitors are located.

      If you haven't included the jQuery library on the page, first do that...

      Code:
      <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
      And then the script...

      Code:
      <script>
          $(function(){
              var today = new Date().getDay(); // returns a zero-based number representing the current day of the week
              var fileName = "t" + today + ".php"; // proper format for the file name
              $("#phpContent").load(fileName); // loads the php file contents into a div with the ID "phpContent"
           });
      </script>
      ^ The above code assumes that you want to load the appropriate php file into a div with the ID of "phpContent", and that the php files are located in the same directory as the web page that calls them. If they are in a different directory, then you will have to alter the 'fileName' variable.
      Signature

      {{ DiscussionBoard.errors[8089725].message }}
      • Profile picture of the author David Beroff
        The suggestions above are probably better, but I did have an idea for a solution that would reside completely on the server: There's probably a timezone field in the GeoIP database by MaxMind that you could use to determine the visitor's date/time without relying on their computer to be set correctly. Just a thought; not necessarily the best one.
        Signature
        Put MY voice on YOUR video: AwesomeAmericanAudio.com
        {{ DiscussionBoard.errors[8090608].message }}
      • Profile picture of the author FirstSocialApps
        Originally Posted by Brandon Tanner View Post

        This is just off the top of my head, so you'll need to test it to make sure it works. It uses Javascript to get the current day of the week, so it will be based on where your website visitors are located.
        Ha I was just going to write the OP a script that used $_POST to pass the clients date to a PHP script that them returned the content. Glad I saw your (better) solution before I wasted my time
        {{ DiscussionBoard.errors[8091509].message }}
      • Profile picture of the author prince55l
        Originally Posted by Brandon Tanner View Post

        This is just off the top of my head, so you'll need to test it to make sure it works. It uses Javascript to get the current day of the week, so it will be based on where your website visitors are located.

        If you haven't included the jQuery library on the page, first do that...

        Code:
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        And then the script...

        Code:
        <script>
            $(function(){
                var today = new Date().getDay(); // returns a zero-based number representing the current day of the week
                var fileName = "t" + today + ".php"; // proper format for the file name
                $("#phpContent").load(fileName); // loads the php file contents into a div with the ID "phpContent"
             });
        </script>
        ^ The above code assumes that you want to load the appropriate php file into a div with the ID of "phpContent", and that the php files are located in the same directory as the web page that calls them. If they are in a different directory, then you will have to alter the 'fileName' variable.
        Great content you put up there. thats a nice solution
        {{ DiscussionBoard.errors[8092591].message }}

Trending Topics