On PHP eighth---using PHP's date function to output a calendar _php tutorial

Source: Internet
Author: User
Tags iso 8601 iso 8601 format
Hello, everyone, the first two days to explain the next RBAC "role-based access control" of the basic idea, do not know whether you have understood its principle, use it handy, in the process of development projects can try to use, this will understand more thoroughly, the above for everyone to write the Rbac class , and not necessarily really suitable for our project development, to write this kind of purpose for everyone to help you understand more thoroughly how RBAC works and how to use ...
Well, you may already think I am wordy, nonsense not much to say, today to explain to you:
On the basis of the known start date and the known end date, use the PHP date function to traverse the output calendar:
Daily life, calendar and our life work is closely related, necessary, so today to everyone to talk about the calendar, you can try to do an electronic calendar, daily reminders of the day to complete the task, or very interesting.
OK, first we look at the date function, the students know that the date function can get the current month and date, but the function of the date function is not limited to this, we use the official PHP manual to check its parameters to see:


Format character description return value example
Day------
Day of the D month, 2 digits with leading zeros 01 to 31
The day of the D-week, the text says, 3 letters Mon to Sun
Day of the J month, without leading 1 to 31
L (Lowercase letter of "L") Day of the week, full text format Sunday to Saturday
N ISO-8601 format number represents the day of the week (PHP 5.1.0 new Plus) 1 (for Monday) to 7 (represents Sunday)
S English suffix, 2 characters st,nd,rd or th, after the number of days per month. Can be used with J.
The day of the W week, the number represents 0 (representing Sunday) to 6 (for Saturday)
Days 0 to 366 in the Z-year
Week------
The week of the year in W ISO-8601 format, starting from Monday (PHP 4.1.0 New) For example: 42 (42nd Week of the year)
Month------
F month, full text format, such as January or March January to December
The month represented by the m number, with a leading 1 to 12
M three-letter abbreviation for month Jan to Dec
n number of months, without leading 1 to 12
t the number of days that a given month should be 28 to 31
Years------
L is a leap year if the leap year is 1, otherwise 0
o ISO-8601 format year number. This is the same value as Y, except if the day of the Week (W) of the ISO belongs to the previous year or the next year. (PHP 5.1.0 New) examples:1999 or 2003
Y 4-digit year full representation for example: 1999 or 2003
Y 2 digits indicate the year for example: 99 or 03
Time------
A lowercase morning and afternoon value am or PM
A uppercase morning and afternoon value am or PM
B Swatch Internet Standard 000 to 999
G-hour, 12-hour format, without leading 1 to 12
G-hour, 24-hour format, without leading 00 to 23
H-hour, 12-hour format, with leading 1 to 12
H-hour, 24-hour format, with leading 000 to 23
I have a leading zero minute number 00 to 59>
S number of seconds, with leading 000 to 59>
Time Zone------
E Time zone identifier (PHP 5.1.0 new) Example: Utc,gmt,atlantic/azores
I is daylight saving time if daylight saving time is 1, otherwise 0
O hours of difference from GMT for example: +0200
The difference between P and GMT (GMT) is separated by a colon between hours and minutes (PHP 5.1.3) For example: +02:00
T local time zone for example: EST,MDT ("translator note" in Windows for the full text format, such as "Easternstandard Time", the Chinese version will show "Chinese standard times").
The number of seconds to offset the Z-time difference. The time zone offset in the west of UTC is always negative, and the time zone offset to the east of UTC is always positive. 43200 to 43200
Full Date/Time------
Date in C ISO 8601 format (PHP 5 new Plus) 2004-02-12t15:19:21+00:00
R RFC 822 Format date For example: Thu, 2000 16:01:07 +0200
U number of seconds since the Unix era (January 1 1970 00:00:00 GMT)

As you can see, PHP's Date function is omnipotent, so let's use the date function to write a calendar:
/*+---------------------------------------------------------------------------------------+
| Calendar class
+-----------------------------------------------------------------------------------------+
| All rights reserved lamp Brother Lian
+-----------------------------------------------------------------------------------------+
| Author: Jie Li (lijie@li-jie.me)
| Last modified time: 2012-05-9 12:30
+-----------------------------------------------------------------------------------------+
*/
Class lampdate{
Private $start _year; Start year
Private $start _month; Start month
Private $start _day; Start date
Private $end _year; End year
Private $end _month; End Month
Private $end _day; End Date
Private $start _time; Timestamp of the start time
Private $end _time; Time stamp for end time
Defining an array of week headers
Private $week = Array ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
Time stamp at the beginning of the start time
Private $start;
Timestamp at the end of the start time
Private $end;
Style sheet CSS file path
Private $css;
The path to the JS event file
Private $script;
/**
* Calendar Class Construction method
* @param start_year start year
* @param start_month Start Month
* @param start_day Start date
* @param end_year End Year
* @param end_month End Month
* @param end_day End Date
* @param CSS Output date style
* Event @param script output date
*/
Public function __construct ($start _year= ', $start _month= ' 1 ', $start _day= ' 1 ', $end _year= ', $end _month= ' 12 ', $end _day= ' to ', $css = ' style.css ', $script = ' js.js ') {
$this->start_year = $start _year; Assign a value to the start year
$this->start_month = $start _month; Assign a value to the start month
$this->start_day = $start _day; Assign a value to a start date
$this->end_year = $end _year; Assign a value to the end year
$this->end_month = $end _month; Assign a value to the end month
$this->end_day = $end _day; Assign a value to an end date
$this->css = $css; CSS for the specified date
$this->script = $script; JS event for the specified date
Gets the timestamp of the start date
$this->start_time = Strtotime ($this->start_year. " -". $this->start_month." -". $this->start_day);
Gets the timestamp of the end date
$this->end_time = Strtotime ($this->end_year. " -". $this->end_month." -". $this->end_day);
Gets the timestamp at the beginning of the start time
$this->start = Strtotime ($this->start_year. " -". $this->start_month." -01 ");
Gets the timestamp of the month of the start time
$this->end = Strtotime ($this->end_year. " -". $this->end_month." -". Date (" T ", $this->end_time));
Gets the number of days from the start date to the end date
$this->day_count = (($this->end)-($this->start))/(24*60*60);
}
/**
* Get the table header for the monthly table
* @param i from the first day after the beginning of the month
* @return return to the head-year-month
*/
Private Function Get_caption ($i) {
Define an empty string
$str = ";
$str. = "









































start+ ($i * (24*60*60))); $m + +) {
$str. = "






















































































";$str. = " "; $str. =" "; $str. =" ";Return to table headerreturn $str;}/*** Output Monthly Week header method* @return return to day of the week table header*/Private Function Get_week () {Define an empty string$str = ";$str. = " "For ($w =0; $w <7; $w + +) {//Traverse output Monday to Sunday $str.=" ";} $str. = "";} $str. = " ";Return to week headreturn $str;}/*** Fill the blanks of the month front* @param i from the first day after the beginning of the start time* @return return blanks for completion*/Private Function Get_space_front ($i) {Define an empty string$str = ";If the first day of the month after the beginning of the start time is Monday, output a TR line breakif (Date ("N", $this->start+ ($i * (24*60*60))) ==1) {$str. = " ";}If the first day of the month is from day I after the beginning of the start time, the output space is traversedif (Date ("D", $this->start+ ($i * (24*60*60))) ==1) {Week N is output n-1 a spacefor ($m =1; $m ";}}Returns the space of the outputreturn $str;}/*** Fill the space on the back end of the month* @param i from the first day after the beginning of the start time* @return return blanks for completion*/Private Function Get_space_back ($i) {Define an empty string$str = ";If the number of days from the first day after the beginning of the start time is the end of the monthif (Date ("Ymd", $this->start+ ($i * (24*60*60))) ==date ("Ymt", $this->start+ ($i * (24*60*60)))) {Week N is output 7-n a spaceFor ($w =date ("N", $this->start+ ($i * (24*60*60))), $w <7; $w + +) {$str. = " ";}}Returns the blanks for completionreturn $str;}/*** Output Current date* @return Return Date*/Private Function Get_date ($i) {Define an empty string$str = ";If the start date is from the first day after the beginning of the start time, the output background is a #7ccd7c color tableif (Date ("Ymd", $this->start+ ($i * (24*60*60))) = = = $this->start_year. $this->start_month. $this->start_day ){$str. = " ";Returns the date of the outputreturn $str;}/*** Blank line for output completion* @param i from the first day after the beginning of the start time* @param t number of rows per month* @return return Blank lines for completion*/Private Function Get_rows ($i,& $t) {Define an empty string$str = ";If the first day of the month after the beginning of the start time is Sundayif (Date ("N", $this->start+ ($i * (24*60*60))) ==7) {Line break$str. = " ";If it is the last day of the month from the first day after the beginning of the start time and is Sundayif (Date ("Ymd", $this->start+ ($i * (24*60*60))) ==date ("Ymt", $this->start+ ($i * (24*60*60))) && date ("N", $this->start+ ($i * (24*60*60))) ==7) {Line number self-reduction$t--;}The number of rows is self-added$t + +;}If it is the last day of the month and the number of rows is less than 6, from the day after the beginning of the start timeif (Date ("Ymd", $this->start+ ($i * (24*60*60))) ==date ("Ymt", $this->start+ ($i * (24*60*60))) && $t <6) {while ($t <6) {Start output line break$str. = " "For ($n =1; $n <=7; $n + +) {//output seven spaces $str.=" ";} $str. = " ";Number of rows from plus one$t + +;}}Blank line for output completionreturn $str;}/*** End the output of the table* @param i from the first day after the beginning of the start time* @param the number of rows in the T date table* @return End Form*/Private Function Get_endtab ($i,& $t) {Define an empty string$str = ";If the last day of the month is the first day after the start dateif (Date ("D", $this->start+ ($i * (24*60*60))) ==date ("T", $this->start+ ($i * (24*60*60)))) {Output End Table$str. = "
";
Get the year and month of the first day of the beginning of the month
$str. = Date ("Y", $this->start+ ($i * (24*60*60))). " ". Date (" M ", $this->start+ ($i * (24*60*60)))." Month ";
$str. = "
". $this->week[$w];
if ($w ==5 | | $w ==6) {
If for Saturday or Sunday, add a small holiday icon
$str. = "
";
If the end date is the first day after the beginning of the start time, the output background is a #ee6363 color table
}elseif (Date ("Ymd", $this->start+ ($i * (24*60*60))) = = $this->end_year. $this->end_month. $this->end_day) {
$str. = "
";
If the current date is the first day after the beginning of the start time, the output background is a #ee2c2c color table
}elseif (Date ("Ymd", $this->start+ ($i * (24*60*60))) = = (Date ("Ymd", Time ()))) {
$str. = "
";
Otherwise output a table with no background color
}else{
$str. = "
";
}
Outputs the date of day I from the beginning of the start time
$str. = Date ("J", $this->start+ ($i * (24*60*60)));
$str. = "
";
and change the number of rows counter to 1
$t = 1;
}
Return to end Table
return $str;
}
/**
* Get styles and events for the output month table
* @return returns the style and events of the month table
*/
Private Function Get_style () {
$str = " ";
$str. = "";
Return file contents
return $str;
}
/**
* Get Date form
* @param i from the first day after the beginning of the start time, the default is 0
* @return Return Date form
*/
Public Function Get_datetab ($i =0) {
Get Styles and Events
$str = $this->get_style ();
Start traversing dates by the number of days from the start date to the end date
For ($i =0, $t =1; $i <= $this->day_count; $i +) {
If for the first day of this month
if (Date ("D", $this->start+ ($i * (24*60*60))) ==1) {
Get table Header
$str. = $this->get_caption ($i);
Get day of the week table header
$str. = $this->get_week ($i);
}
Fill the blanks in front of the table
$str. = $this->get_space_front ($i);
Get Date
$str. = $this->get_date ($i);
Spaces following the completion table
$str. = $this->get_space_back ($i);
Define the number of rows for a date table
$str. = $this->get_rows ($i, $t);
End Table
$str. = $this->get_endtab ($i, $t);
}
Return to date table
return $str;
}
}


I added a more full comment, we have what do not understand the place can be a thread to answer questions, or I will be in the next post to give you a detailed explanation of the problems we encountered together, please look forward to ....

Output effect:

I send the code up, you can download the study under:

Calendar Code date_class.rarhttp://bbs.lampbrother.net/job.php?action=download&aid=21095

Author Zdrjlamp

http://www.bkjia.com/PHPjc/478235.html www.bkjia.com true http://www.bkjia.com/PHPjc/478235.html techarticle Hello, everyone, the first two days to explain the next RBAC "role-based access control" of the basic idea, do not know whether everyone has understood its principle, use is handy, ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.