Show different content depending on month

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

I have sorted it!
Thanks for all the help guys, much appreciated :slight_smile:

I am glad you managed to sort the problem.

Did you copy the contents of index2.php to a default.php file and use the following:

Hi

I have this set up at the moment within my index.php file

<?php
session_start();
if($_REQUEST['pass'] == "xxxx" || $_REQUEST['pass'] == "XXXX"):
$_SESSION['logged'] = 1;
include strtolower(date('F')).'.php';
exit;
endif;
?>

Problem now is, it looks like I will have to specify a directory, as the content for MAY, JULY and OCTOBER are slightly different.

So when a user logs in, they need to go to /may

Is this possible? Is this the correct way of doing this?

Thanks again for all help, much appreciated!

Try this:

    $month = date('n');
    // $month = 13; // just testing to see if invalid motnh
    switch( (int) $month ){
        case 1 :   include 'january.php'; // same directory
        break;
        case 2 :   include '../february.php'; // parent directory
        break;
        case 3 :   include '/march.php'; // root directory
        break;
        case 4 :   include '/test/test-001/april.php';
        break;
        case 5 :   include '/test/test-002/may.php';
        break;
        case 6 :   include '/test/test-003/june.php';
        break;
        //...
        //...
        //...
        default: include 'not-a-month-just-blurb.php';
  } // endswitch(...)        

Edi:
It is verbose but easy to read and also easy to adjust different paths. Also easy to setup test paths.

Hi John

Thanks for the response. Unfortunately that does not seem to work. I get errors. I cannot get it to work.

I used the below for a specific file and this worked.

<?php
session_start();
if($REQUEST['pass'] == "xxxx" || $REQUEST['pass'] == "XXXX"):
$_SESSION['logged'] = 1;
include strtolower(date('F')).'.php';
exit;
endif;
?>

But as I mentioned I need to now also specify a directory for example /may… /june… july.

As each month calls in different content from include files.

Not sure if I am doing this really wrong, as I feel their must be a more streamlined solution.

Thanks again.