How can I get a PHP include to update automatically after a set date?

Hi guys,

I maintain a number of website for sports athletes and on their websites I have an include for their next event - just a small PHP file with when and where the next event is.

After each event I have to manually update the include to the clients’ next event which is a bit of a pain and quite time-consuming.

Is there any way I can set up the include script to automatically update with a different file to include after a set date?

Or, even better, can I set up the include itself to display the updated content after a specific date?

This is what the include contains…

<img class="event" src="/images/eventPicture.png" />
<p class="nextEvent">Location<br />
Series<br />
1-3 January 2013</p>

Thank you very much and I hope to hear from you!

SM

Something like this would work:


<img class="event" src="/images/eventPicture.png" />
<p class="nextEvent">
<?php
$events = array(
    // end date => description
    '2013-05-02' => 'Location<br />Series<br />1st of May 2013',
    '2013-06-02' => 'Location 2<br />Some series<br />1st of June 2013',
);
$now = new DateTime();
$eventShown = false;
foreach($events as $date => $event)
{
    if ($new < new DateTime($date))
    {
        $eventShown = true;
        echo $event;
        break;
    }
}
if (!$eventShown)
{
    echo "No upcoming events";
}
?>
</p>

This will show the first message up until the second of may (so it also shows on actual day of the event), and then switches to the next one. This way you can just load a bunch of events in the script and let the logic decide which one is shown at a particular point in time. After they are all done just load a new bunch, etc.

Hi Scallio!
Brilliant - that works, thank you so much!
You’ve just saved me loads of time!
Thank you very much again! - top shooter!
SM

Hi Rémon,

I used your code and today it should’ve updated but is still just displaying the first date in the list. Have I done something wrong?

I edited the code you sent slightly but essentially it is the same. Here it is…


<?php
$events = array(
    // end date => description
    '2013-04-22' => '<img class="circuit" src="sano.png" /><p class="next">Sano<br />Series<br />20-21 April 2013</p>',
    '2013-05-06' => '<img class="circuit" src="liano.png" /><p class="next">Liano<br />Series<br />4-5 May 2013</p>',
    '2013-05-20' => '<img class="circuit" src="roah.png" /><p class="next">Roah<br />Series<br />18-19 May 2013</p>',
    '2013-06-03' => '<img class="circuit" src="gek.png" /><p class="next">Sano<br />Series<br />1-2 June 2013</p>',
    '2013-06-17' => '<img class="circuit" src="al.png" /><p class="next">Alaniz<br />Series<br />15-16 June 2013</p>',
    '2013-06-24' => '<img class="circuit" src="grs.png" /><p class="next">Glors<br />Series<br />22-23 June 2013</p>',
    '2013-06-08' => '<img class="circuit" src="lalall.png" /><p class="next">Lhall<br />Series<br />6-7 July 2013</p>',
    '2013-07-15' => '<img class="circuit" src="caletto.png" /><p class="next">Cetto<br />Series<br />13-14 July 2013</p>',
    '2013-07-22' => '<img class="circuit" src="orta.png" /><p class="next">Orona<br />Series<br />20-21 July 2013</p>',
    '2013-08-26' => '<img class="circuit" src="clayeon.png" /><p class="next">Claon<br />Series<br />24-25 August 2013</p>',
    '2013-09-23' => '<img class="circuit" src="pf-int.png" /><p class="next">PF<br />Series<br />21-22 September 2013</p>',
    '2013-10-07' => '<img class="circuit" src="sao.png" /><p class="next">Sano<br />Series<br />5-6 October 2013</p>',
    '2013-10-14' => '<img class="circuit" src="shenton.png" /><p class="next">Son<br />Series<br />12-13 October 2013</p>',
    '2013-11-04' => '<img class="circuit" src="casetto.png" /><p class="next">Ctto<br />Series<br />2-3 November 2013</p>',
    '2013-11-17' => '<img class="circuit" src="bahin.png" /><p class="next">Bahin<br />Series<br />15-16 November 2013</p>',
    '2014-05-01' => '<p class="nextRace">TBA</p>',
);
$now = new DateTime();
$eventShown = false;
foreach($events as $date => $event)
{
    if ($new < new DateTime($date))
    {
        $eventShown = true;
        echo $event;
        break;
    }
}
if (!$eventShown)
{
    echo "<p><strong>TBA</strong></p>";
}
?>

~One of these things is not like the other…~

Hi there!

Thanks for the reply - how do you mean one of those things is not like the other?

Look closer at the variable names I highlighted in red above. You’ve got $nOw and $nEw… The script defines $now as the current date, but $new is not initialized. turn the $new into $now, and the script works as intended.

You genius! Sorry, I originally looked at your correction on my phone and couldn’t see the change in color in the code!

Thank you so much - you’ve made my day :slight_smile:

Thanks again, top work!