PHP Calendar code, PHP Calendar control, PHP calendar program
First of all, the implementation of this PHP calendar code ideas, the analysis of the difficulties in where.
The implementation of the calendar under PHP is largely dependent on PHP's powerful time and date functions. including date () and mktime (), I now find that PHP is the most BT function of the date (), Dizzy ~ ~ ~ I used to think it was the heap of data manipulation and file management functions. Implement calendar functions, mainly To figure out how it works, it's easy to write a program.
It may be that the calendar is complex at first, with changes in the week of the month, as well as leap years. But careful analysis, the implementation of the calendar is not too complicated. The most basic of a calendar is definitely the top of the week logo, for programming convenience and synchronization with international habits, we have Sunday as the beginning of the week, Saturday as the end. This implementation is relatively simple, an array with the loop output is possible. The remaining major problem is the display of the date.
The display of dates is mainly to catch several questions:
1. How many days are there in this month?
2. What is the first day of the month?
As for the one months there are many weeks, this basic not to consider, because we in the calendar date display in the new week (that is, a new line, you can find a calendar to study, total not 31 days all in a row of output bar) processing completely does not need to use the monthly number of weeks, just to determine whether a day is Sunday, Add a bit of HTML ingenuity, before the date of Sunday is displayed before the output of a new line of TR, which is more convenient than the number of weeks to judge.
There are only the first two questions left. The meaning of the first question is needless to say, actually getting this data is simple, and can be achieved by invoking the date () and mktime () functions. You can use the following call:
Date ("T", Mktime (0,0,0, $month, 1, $year));
Here, Mktime (0,0,0, $month, $day, $year) The first three 0 mark hours, minutes and seconds, generally less used, direct fill 0, behind three parameters is the identification of the month, day, This function returns a timestamp in UNIX format, which has no direct meaning and needs to be processed with the date () function. date (); function has many formatting identifiers, that is "T" that ... Refer to the PHP documentation for details. Date ("T", Mktime (0,0,0,$ month, $day, $year) returns the one-month date that is identified by the timestamp of Mktime (), ranging from 28-31. Note that a specific date must be provided here, Like number 1th.
As for the second question, it is also very important because the number of weeks 1th per month is not the same. So the calendar output is to have to output a space before 1th per month, to ensure that the number of weeks 1th per month is true (for example, 1th in a month is the Week 4 I have to output spaces in the corresponding position from Sunday to Wednesday). This data is also obtained using the date () and Mktime () functions:
Date ("W", Mktime (0,0,0, $month, 1, $year))
This set of functions returns the number of weeks of 1th per month, with a return value from 0 to 6, which represents Sunday through Saturday.
In this way, the basic data required for a calendar is available, and you can start implementing the calendar.
The first is to get the month and the year, this thing with the HTML form there are many different implementation methods, I used the input box, it is relatively simple, and then the action of the form is $script_name, that is, the script itself. The method is post.
After you get the month and year, you can output the main table part of the calendar, first you can use an array to output the day of the week, from Sun to the SAT. Then start outputting all the dates, which is a loop:
for ($day =1; $day <=date ("T", Mktime (0,0,0, $month, 1, $year)); $day + +)
{....}
The braces include several conditional judgments. The first is to get the number of weeks of the first day of the month, through a loop, before the number of weeks before the corresponding position of the output space. The second one is to determine whether the day is Sunday, if it is to output a TR line.
'; Echo ''. $year. ' Year ' $month. ' Month '. ''; Echo '' for ($i = 0; $i < 7; $i + +) {echo ''. $week [$i]. '';} Echo ''; Echo '' for ($j = 0; $j < $start _weekday; $j + +) {echo ''. $j. '';} while ($k <= $days) {if ($k = = Date (' d ')) {echo ''. $k. ''; }else{Echo ''. $k. ''; } if (($j + 1)% 7 = = 0) {echo ''; } $j + +; $k + +;} while ($j% 7 = 0) {echo ''. $j. ''; $j + +;} Echo ''; Echo '' Echo '".' << '. '' Echo '".' < '. ''; Echo ''; Echo '; Echo '' Echo '".' >> '. '' Echo '".' > '. ''; Echo ''; Echo ''; function lastyear ($year, $month) {$year = $year-1; return "year= $year &month= $month";} function Lastmonth ($year, $month) {if ($month = = 1) {$year = $year-1; $month = 12; }else{$month--;} return "year= $year &month= $month";} function Nextyear ($year, $month) {$year = $year +1; return "year= $year &month= $month";} function Nextmonth ($year, $month) {if ($month = =) {$year = $year +1; $month = 1; }else {$month + +;} return "year= $year &month= $month";}? >