Skip to content

Instantly share code, notes, and snippets.

@Rudis1261
Last active December 26, 2015 18:19
Show Gist options
  • Save Rudis1261/7193896 to your computer and use it in GitHub Desktop.
Save Rudis1261/7193896 to your computer and use it in GitHub Desktop.
So you want to be able to see what day it is and then fill an array to display something later on. This is how I did it. I am using this to track events happening within the following week. It could be used for a calendar system and so on.
<?php
# Define the days of the week
$days = array(
'Friday',
'Saturday',
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday'
);
# Shift the days until it matches today :-D
while(current($days) !== date('l', time()))
{
$days[] = array_shift($days);
}
# You can then do anything which you fill the array up with and loop through it thereafter.
# A good example is for a meeting system, or a calender system.
# So if today was Tuesday I could add meetings for today and thursday
# Let's say we have a meetings array
$meetings = array( "week" => array() );
# Let's loop though our days array and create the weeks calender per day
foreach($days as $day)
{
$meetings["week"][$day] = array();
}
# using a small sample set, let's add data to the array to iterate through later on.
$meetings["week"]['Monday'][] = "Get a job";
$meetings["week"]['Friday'][] = "TGIF";
$meetings["week"]['Tuesday'][] = "Take a run";
# Now even though the order we inserted the up and coming events, we can cycle through the
# days array and the events will be in order of first possible occurrence.
echo "<h3>Coming in the next week:</h3>";
# Loop through the days
foreach($meetings["week"] as $dayOfWeek=>$dayEvents)
{
# Now I just want to make sure the day has an event
if (!empty($dayEvents))
{
echo "<p>";
echo "<b>" . $dayOfWeek . "</b><br />";
echo "<ul>";
# We can now cycle through the events of the day and group it for the day
foreach($dayEvents as $event)
{
echo "<li>" . $event . "</li>";
}
echo "</ul>";
echo "</p>";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment