PHP - Content depending on day of week

by 8 replies
10
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..

.. 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..
#programming #content #day #depending #php #week
  • 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
  • Hey Tom,

    I had a feeling that was the case.

    What do you recommend.. Javascript?

    I'll pay for a fully working solution!!
  • 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
  • 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
    • [1] reply
    • 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.
      • [ 3 ] Thanks
      • [3] replies

Next Topics on Trending Feed