Last active
December 26, 2015 18:19
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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