PHP calendars can be useful. You can do things as simple as showing the date, and as complex as setting up an online booking system. This article shows how to generate a simple PHP calendar. When you understand how to do this, you will be able to apply the same concepts to complex calendars you may need.
The first part of the code sets some variables that are needed later in the script. The first step is to find out what the current date is using the time () function. Then, you can use the date () function to format the date appropriately for the $day, $month and $year variables. Finally, the code generates the name of the month, which is the title of the calendar.
Source-https://www.thoughtco.com/simple-php-calendar-2693849
The first part of the code sets some variables that are needed later in the script. The first step is to find out what the current date is using the time () function. Then, you can use the date () function to format the date appropriately for the $day, $month and $year variables. Finally, the code generates the name of the month, which is the title of the calendar.
Days of the Week
Here you take a close look at the days of the month and prepare to make the calendar table. The first thing is to determine on which day of the week the first of the month falls. With that knowledge, you use the switch () function to determine how many blank days are needed in a calendar before the first day.
Next, count the total days of the month. When you know how many blank days are needed and how many total days are in the month, the calendar can be generated.
03
of 05
Headings and Blank Calendar Days
The first part of this code echoes the table tags, the month name and the headings for the days of the week. Then it starts a while loop that echoes empty table details, one for each blank day to count down. When the blank days are done, it stops. At the same time, the $day_count is going up by 1 each time through the loop. This keeps count to prevent putting more than seven days in a week.
04
of 05
Days of the Month
Another while loop fills in the days of the month, but this time it counts up to the last day of the month. Each cycle echoes a table detail with the day of the month, and it repeats until it reaches the last day of the month.
The loop also contains a conditional statement. This checks if the days of the week have reached 7—the end of the week. If it has, it starts a new row and resets the counter back to 1.
No comments:
Post a Comment