Show different content depending on month

Hi

I need the ability for a site I am working on to show different content depending on the month. The site is for a kids activity camp, so they have different content depending on the holiday period eg. February half term, Easter, May half term, summer, October half term.

The current site you have to login from the front page then once logged in it needs to direct the user to the correct holiday period content.

Is this possible using PHP or perhaps Jquery?. I really hope someone can help me.

Much appreciated.

Yes it is possible; but may be harder if you are using a CMS

Hi

Their will be no CMS, are you able to help me. I only have experience with HTML and CSS :frowning:

Thanks

PHP has a very good date() function: http://php.net/manual/en/function.date.php

Also the following switch() statement that can include related “month” files which may be PHP, CSS, HTML, etc

Try this:

    $month = date('n'); // 4 == April, 5 = May, etc  // no leading zero
    switch( $month ){
        case 1 :        include 'january.php'
        break;
        case 2 :        include 'february.php'
        break;
        case 3 :        include 'march.php'
        break;
        case 4 :        include 'april.php'
        break;
        case 5 :        include 'may.php'
        break;
        case 6 :        include 'june.php'
        break;
        case 7 :        include 'july.php'
        break;
        case 8 :        include 'august.php'
        break;
        case 9 :        include 'september.php'
        break;
        case 10:        include 'october.php'
        break;
        case 11:        include 'november.php'
        break;
        case 12:        include 'december.php'
        break;
    }

1 Like

or a shorter version:

// January.php, February.php, etc.
include date('F') . '.php';

:smiley:

8 Likes

Great many thanks I will give that a go

Thanks again.

Hi

Can’t seem to get any of the ones posted to work.

What happens is a user needs to login from the home page (index.php) and on this page this code has been added

<?php session_start(); if($_REQUEST['pass'] == "xxxxxx" || $_REQUEST['pass'] == "XXXXXX"): $_SESSION['logged'] = 1; header('Location:index2.php'); exit; endif; ?>

This then takes them to the content of the site with all the navigation etc.

Do I add the month content switcher to index2.php?

When I added this I get lots of syntax errors

<?php $month = date('n'); // 4 == April, 5 = May, etc // no leading zero switch( $month ){ case 1 : include 'january.php' break; case 2 : include 'february.php' break; case 3 : include 'march.php' break; case 4 : include 'april.php' break; case 5 : include 'may.php' break; case 6 : include 'june.php' break; case 7 : include 'july.php' break; case 8 : include 'august.php' break; case 9 : include 'september.php' break; case 10: include 'october.php' break; case 11: include 'november.php' break; case 12: include 'december.php' break; } ?> Really hope someone can assist and help me to get this work.

Many Thanks

You need a semicolon at the end of each of the include lines.

1 Like

Also, why are you using the long switch rather than the nice short version that @Lemon_Juice offered?

Why are you using a switch statement when a single include can do it

include strtolower(date('F')).'.php';

Hi

I cannot get any of these to work. As I mentioned once a user has logged in at the moment it diverts them to index2.php.

Depending on the month of the year I need the content to be displayed automatically for a particular month, May, June etc.

I added this to the top of index.2php HTML

<?php include strtolower(date('M')).'may.php';?>

But it did not display the May content. I need the process to be automatic, I don’t know PHP very well so please forgive my ignorance. I am more than happy to post a link through PM if that helps?

Look forward to hearing from you guys as I really need to get this to work.

Thanks again.

If you do this, does it look like a file you might want to include?

$test = strtolower(date('M')).'may.php';
echo $test;

Hi

I added this

<?php $test = strtolower(date('M')).'may.php'; echo $test;?>

Refreshed index2.php and it is still showing index2.php’s content.

I am testing the site locally as well.

I tried john_betongs solution

<?php $month = date('5'); // 4 == April, 5 = May, etc // no leading zero switch( $month ){ case 1 : include 'january.php'; break; case 2 : include 'february.php'; break; case 3 : include 'march.php'; break; case 4 : include 'april.php'; break; case 5 : include 'may.php'; break; case 6 : include 'june.php'; break; case 7 : include 'july.php'; break; case 8 : include 'august.php'; break; case 9 : include 'september.php'; break; case 10: include 'october.php'; break; case 11: include 'november.php'; break; case 12: include 'december.php'; break; } ?>

And this is working only if I change $month = date(‘n’); to $month = date(‘5’);

But I need the process to be automatic, is this at all possible?

Thanks

I prefer to use PHP’s require because if there is a problem an error is produced. Also while testing I prefer to use PHP’s die; because it immediately halts execution and displays any messages. Without the PHP die; and depending on the error configuration error messages are mostly likely to be hidden.

@mumford1, try this:

<?php require strtolower(date('M')) . '.php'; ?>

is what I suggested - not date(‘M’) and not adding may before the .php

date(‘F’) - will currently return ‘April’ then strtolower converts it to ‘april’ to which the concatenation makes it ‘april.php’. Tomorrow you’d get ‘may.php’;

strtolower(date(‘M’)).‘may.php’ - currently gives ‘aprmay.php’ and tomorrow it will give ‘maymay.php’

1 Like

Hi Felgall

Got the content showing now! But the problem is it is also showing index2.php’s content beneath? How can I remove that?

Many Thanks

I apologise for supplying script which was not tested.

The problem was date('n'); returns a string and not an integer.

A quick solution, which I have tested :slight_smile: is to force the string to be an integer:

Update:

$month = date('n'); // "4" == April, "5" = May, etc  // string with no leading zero
    switch( (int) $month ){
        case 1 :        include 'january.php'
        break;
        case 2 :        include 'february.php'
        break;
        ...
        ...
        ...
  } // endswitch(...)



Thanks for that

As mentioned above it is also showing index2.php’s content as well I need to remove this and just show may’s content.

Thanks

I have sorted it!

Thanks for all the help guys, much appreciated :slight_smile:

1 Like