Jump to content

[SOLVED] Displaying months in order


jakeoh

Recommended Posts

I have an two-level array that contains, at the first level, a month of the year; the second level contains events held that month (name, description, start date, end date).

 

I want to display this array in a table (events to come).

 

The code I am using right now is this:

 

<table class="calendrier">

<?php
foreach($month_list as $month)
{
echo "<tr><td class='calendrier_mois' colspan='3'><h3>".$month['month_name']." </h3></td></tr>";
echo "<tr class='calendrier_head'>";
echo "<td class='calendrier_date'>Dates</td>";
echo "<td class='calendrier_even'>Événements</td>";
echo "<td class='calendrier_lieu'>Lieu</td>";
echo "</tr><tr>";

foreach($month['event_list'] as $event)
{
echo "<td class='calendrier_01'>".$event['event_startdate']."</td>";
echo "<td class='calendrier_02'>".$event['event_description']."</td>";
echo "<td class='calendrier_03'>".$event['event_location']."</td>";
echo "</tr>";
}

}
?>

</table>

 

This is all fine, but I want to start with the current month; my array is ordered from January to December, so they are obviously ordered this way presently.

 

How can I use PHP to start with the current month, and then cycle through the array and come back to January after December?

 

Hope it's clear.

 

Jake

Link to comment
Share on other sites

have two loops.  the first one starts at the index of the current month and goes to december.  the second one starts at the beginning and goes up to but not including the current month

I understand your technique. However I'm not sure how to code it. How can the foreach start on the current month?
Link to comment
Share on other sites


<?php 
$month_list = array();

for( $i=1;$i<13;$i++) {
$month_list[$i] = date("F", mktime(0, 0, 0, $i, 1, 2008));
}
print_r($month_list);
$newmonthlist = $month_list;
foreach($newmonthlist as $monthnumber=>$month) {
   if( $monthnumber < date("n")) {
	unset($newmonthlist[$monthnumber]);
	$newmonthlist[$monthnumber] = $month;
   }
}
echo "<br>";
print_r( $newmonthlist);
?>

 

this should work for you...

 

PS: we unsetting the months and setting the same values to them again in next line, but the difference here is it is an associative array and it will add elements at the bottom, so when u foreach they will come last...

 

I have tried to imitate your month list, just fill it in with ur variable.

Link to comment
Share on other sites

<?php 
$month_list = array();

for( $i=1;$i<13;$i++) {
$month_list[$i] = date("F", mktime(0, 0, 0, $i, 1, 2008));
}
print_r($month_list);
$newmonthlist = $month_list;
foreach($newmonthlist as $monthnumber=>$month) {
   if( $monthnumber < date("n")) {
	unset($newmonthlist[$monthnumber]);
	$newmonthlist[$monthnumber] = $month." ".(date("Y")+1);
   } else {
        $newmonthlist[$monthnumber] = $month." ".date("Y");
   }
}
echo "<br>";
print_r( $newmonthlist);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.