Show different content depending on month

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.

Please supply the code that does not work, the error messages and the required include files (with paths).

1 Like

Hi John

The process needs to be automatic depending on the month. If it is OK with you can I send you a PM to a dev link?

This worked

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

But now need to show content within directories.

Sorry if I am not explaining properly.

Many Thanks

How are the names for the directories arranged and what are the filenames within them? Is there a pattern?

Hi

Most of the pages will have the same content. Bu depending on the month one of the include files I have needs to change content and also a page called ‘term-dates.php’ needs it content changing depending on the month.

So I was thinking of including directories such as /may, /feb etc that contain more or less the same content just with a few small changes.

www.sitename.com/may/term-dates.php

Thanks

So you would simply put the month name in the file path in place of the directory. Something like:-

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

Which gives you:-

include '/may/content.php';

Or were you thinking of a redirect?

header('location:  '/'.strtolower(date('F')).'/index.php');

The principle being that function, strtolower(date('F')) will give you the current date in lower case, so you just insert that wherever you need the month in a string/path or whatever.

Note that 'F' will be the full month name, as in January. If you want Jan, Feb etc, use 'M' instead.

1 Like

You need to include the absolute path to the web root. The / path means the server root directory not the site root.

Yes, modify the path to whatever it should be. I often use something like:-

include $_SERVER["DOCUMENT_ROOT"].'/includes/filename.php';

unless you want the server root.

Hi Guys

I am really struggling with this, I think you need to see the setup, so I was wondering if I could PM with link to the site and perhaps FTP access?

Really appreciate your help.

thanks

the integrity of the server is more important than any development task. Don’t hand out server authentication details to random people on the Internet. I shouldn’t even have to say that.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.