Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Change content based on Day of Week.

Hi its driving me mad, i can do a php script to check day of week and with if and elseif echo out words, But where i am struggling is trying to get it to change content on a page depending on the day. Any advice

4 Answers

Here's a quick and easy solution I put together

    // first we use php's date command to find out the day of the week
$date = date('l');

// our switch statement will assess which day of the week it is and
// assign our $content as assigned.
switch ($date) {
  case 'Sunday':
    $content = "Today is Sunday";
    break;
  case 'Monday':
    $content = "Today is Monday";
    break;
  case 'Tuesday':
    $content = "Today is Tuesday";
    break;
  case 'Wednesday':
    $content = "Today is Wednesday";
    break;
  case 'Thursday':
    $content = "Today is Thursday";
    break;
  case 'Friday':
    $content = "Today is Friday";
    break;
  case 'Saturday':
    $content = "Today is Saturday";
    break;
}


// display our content regardless of day 
echo $content;

Thanks for this Lee. This is exactly what I was looking for. Currently just adding a few very basic PHP tweaks to our standard website. Think this with statements changed to something more fitting really helps it to stand out!

Matt Campbell
Matt Campbell
9,767 Points

Put what you want to show up in the result of the if statement. You doing this in WP or anything? What code you got so far? Pop it a codepen and we can all have a look.

Sorted it now but its massive with alot of repeated content if you could see if i could slim it down would be good. It won't let me copy from Cloud9 for some reason so here is a snippet

<?php
date_default_timezone_set('Europe/London');
$dates = array ('
<div><h3>blah</h3><ul><li><figure>image<figcaption
></figcaption></figure></li></ul></div>,

That is for sunday then next in the array is the same again with same tags but different content for monday then so on for the remainder of the week.

Any help would be great

forgot the last php command echo $date[('w')];

as i say it works but its bulky repeated tags

Erik Nemesis
Erik Nemesis
13,356 Points

Dude, use templates (or partials)

Might I suggest you to look for a template engine such as Twig? I know it's outside of the course material but you might learn one or two things from it by trying by yourself.

The code above could be easily expanded so that you have $figure and $figcaption set for every day. Then you could use your array to reference the variables you've set in your switch statement.