Home > Backend Development > PHP Tutorial > A brief discussion on the eighth bullet of PHP---Using PHP's date function to output calendar_PHP tutorial

A brief discussion on the eighth bullet of PHP---Using PHP's date function to output calendar_PHP tutorial

WBOY
Release: 2016-07-13 17:50:53
Original
1108 people have browsed it

Hello, everyone, I explained to you the basic idea of ​​RBAC [Role-Based Access Control] two days ago. I don’t know if you understand its principle and whether it is easy to use. You can try it during the development of the project. By using it, you will understand more thoroughly. The RBAC class written for you above is not necessarily suitable for our project development. The purpose of writing this class is just to help you understand more thoroughly the working principle of RBAC and How to use...
Okay, you may have already thought that I am verbose, so I won’t say any more nonsense. I will explain it to you today:
Based on the known start date and known end date, use PHP's date function to traverse the output calendar:
In daily life, calendars are closely related to our lives and work and are indispensable. So after telling you about calendars today, you can try to make an electronic calendar to remind yourself of the tasks to be completed that day. It is still very interesting.
Okay, first let’s learn about the date function. Students all know that the date function can obtain the current year, month and day, but the function of the date function is not limited to this. Let’s check its parameters through the official PHP manual:


format character description return value example
Day --- ---
d The day of the month, a 2-digit number with leading zeros 01 to 31
D Day of the week, text representation, 3 letters Mon to Sun
j Day of the month, no leading zero 1 to 31
l (lowercase letter "L") Day of the week, complete text format Sunday to Saturday
N The day of the week represented by numbers in ISO-8601 format (newly added in PHP 5.1.0) 1 (meaning Monday) to 7 (meaning Sunday)
S The English suffix after the day of the month, 2 characters st, nd, rd or th. Can be used with j
w The day of the week, the number represents 0 (indicating Sunday) to 6 (indicating Saturday)
z The day of the year 0 to 366
Day --- ---
W The week number in the year in ISO-8601 format, each week starts on Monday (newly added in PHP 4.1.0) For example: 42 (the 42nd week of the year)
Month --- ---
F month, complete text format, such as January or March January to December
m The month represented by the number, with leading zeros 01 to 12
M is the three-letter abbreviation for the month Jan to Dec
n Month represented by number, no leading zero 1 to 12
t The number of days in a given month 28 to 31
Year --- ---
L Is it a leap year? If it is a leap year, it is 1, otherwise it is 0
o Year numbers in ISO-8601 format. This is the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used. (Newly added in PHP 5.1.0) Examples: 1999 or 2003
Y 4-digit complete year. For example: 1999 or 2003
y Year represented by 2 digits For example: 99 or 03
Time --- ---
a Lowercase morning and afternoon values ​​am or pm
A Uppercase morning and afternoon values ​​AM or PM
B Swatch Internet standard time 000 to 999
g Hour, 12-hour format, no leading zeros 1 to 12
G hours, 24-hour format, no leading zeros 0 to 23
h hour, 12 hour format, with leading zeros 01 to 12
H hour, 24 hour format, with leading zeros 00 to 23
i Minutes with leading zeros 00 to 59>
s Number of seconds, with leading zeros 00 to 59>
Time zone --- ---
e Time zone identifier (newly added in PHP 5.1.0) For example: UTC, GMT, Atlantic/Azores
I Whether it is daylight saving time. If it is daylight saving time, it is 1, otherwise it is 0
O The number of hours difference from Greenwich Mean Time For example: +0200
The difference between P and Greenwich Mean Time (GMT), there is a colon separating hours and minutes (newly added in PHP 5.1.3) For example: +02:00
T The time zone where this machine is located For example: EST, MDT ([Translator's Note] In complete text format under Windows, such as "EasternStandard Time", the Chinese version will display "China Standard Time").
Z Time difference offset in seconds. Time zone offsets west of UTC are always negative, and time zone offsets east of UTC are always positive. -43200 to 43200
Complete date/time --- ---
c Date in ISO 8601 format (newly added in PHP 5) 2004-02-12T15:19:21+00:00
r RFC 822 format date For example: Thu, 21 Dec 2000 16:01:07 +0200
U Number of seconds since the Unix epoch (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
+------------------------------------------------- ----------------------------------------+
| Copyright Lamp Brothers
+------------------------------------------------- ----------------------------------------+
| Author: Li Jie (lijie@li-jie.me)
| Last modified: 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; //Time stamp of start time
private $end_time; //Time stamp of end time
//Define weekly table header array
private $week = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
//The timestamp of the beginning of the start time
private $start;
//Start time and end of month timestamp
private $end;
//Style sheet CSS file path
private $css;
//Path of 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
* @param script Output date event
*/
public function __construct($start_year='2012',$start_month='1',$start_day='1',$end_year='2012',$end_month='12',$end_day='31',$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 starting month
$this->start_day = $start_day; //Assign a value to the 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 the end date
$this->css = $css; //CSS for specified date
$this->script = $script; //JS event for specified date
//Get the timestamp of the start date
$this->start_time = strtotime($this->start_year."-".$this->start_month."-".$this->start_day);
//Get the timestamp of the end date
$this->end_time = strtotime($this->end_year."-".$this->end_month."-".$this->end_day);
//Get the timestamp at the beginning of the start time
$this->start = strtotime($this->start_year."-".$this->start_month."-01");
//Get the timestamp of the start time and end of the month
$this->end = strtotime($this->end_year."-".$this->end_month."-".date("t",$this->end_time));
//Get the number of days from the start date to the end date
$this->day_count = (($this->end)-($this->start))/(24*60*60);
}
/**
* Get the header of the monthly table
* @param i The number of days since the beginning of the month
* @return Return to table header --year--month
*/
private function get_caption($i){
//Define an empty string
$str ='';
$str.= "

";
$str.= "";
$str.= "";
$str.= "";
//Return to header
return $str;
}
/**
* Method to output monthly week header
* @return Return to the week 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 the head of the week
return $str;
}
/**
* Complete the space at the front of the month
* @param i The i-th day after the beginning of the month
* @return Returns the completed space
*/
private function get_space_front($i){
//Define an empty string
$str='';
//If the i-th day after the beginning of the month is Monday, output a tr line break
if(date("N",$this->start+($i*(24*60*60)))==1){
$str.= "";
}
//If the i-th day after the beginning of the month is the first day of the month, traverse the output space
if(date("d",$this->start+($i*(24*60*60)))==1){
//On week n, output n-1 spaces
for($m=1;$mstart+($i*(24*60*60)));$m++){
$str.= "
";
}
}
//Return the output space
return $str;
}
/**
* Complete the spaces at the back end of the month
* @param i The i-th day after the beginning of the month
* @return Returns the completed space
*/
private function get_space_back($i){
//Define an empty string
$str='';
//If the number of days from the i-th day after the beginning of the start time is the end of the month
if(date("ymd",$this->start+($i*(24*60*60)))==date("ymt",$this->start+($i*(24*60* 60)))){
//On week n, output 7-n spaces
for($w=date("N",$this->start+($i*(24*60*60)));$w<7;$w++){
$str.= "";
}
}
//Return the completed space
return $str;
}
/**
* Output the current date
* @return return date
*/
private function get_date($i){
//Define an empty string
$str='';
//If the i-th day after the first month of the start time is the start date, output a table with the background color #7CCD7C
if(date("Ymd",$this->start+($i*(24*60*60)))==$this->start_year.$this->start_month.$this->start_day) {
$str.= "";
//Return the output date
return $str;
}
/**
* Output the completed empty line
* @param i The i-th day after the beginning of the month
* @param t Number of rows output per month
* @return Returns the completed empty line
*/
private function get_rows($i,&$t){
//Define an empty string
$str='';
//If the i-th day after the start of the month is Sunday
if(date("N",$this->start+($i*(24*60*60)))==7){
//Line break
$str.= "";
//If the i-th day after the beginning of the month is the last day of the month and is a Sunday
if(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){
//The number of rows is decremented
$t--;
}
//The number of lines is incremented
$t++;
}
//If the i-th day after the start of the month is the last day of this month and the number of rows at this time is less than 6
if(date("ymd",$this->start+($i*(24*60*60)))==date("ymt",$this->start+($i*(24*60* 60))) && $t<6){
while($t<6){
//Start outputting newline
$str.= "";
for($n=1;$n<=7;$n++){
//Output seven spaces
$str.= "";
}
$str.= "";
//The number of rows increases by one
$t++;
}
}
//Output the completed empty line
return $str;
}
/**
* End the output of the table
* @param i The i-th day after the beginning of the month
* @param t The number of rows in the date table
* @return end form
*/
private function get_endtab($i,&$t){
//Define an empty string
$str='';
//If the i-th day after the start date is the last day of this month
if(date("d",$this->start+($i*(24*60*60)))==date("t",$this->start+($i*(24*60* 60)))){
//Output end form
$str.= "
";
//Get the year and month after the i-th day of the beginning of the start time
$str.= date("Y",$this->start+($i*(24*60*60)))."Year".date("m",$this->start+($i* (24*60*60)))."month";
$str.= "
".$this->week[$w];
if($w==5 || $w==6){
//If it is Saturday or Sunday, add a small holiday icon
$str.= "
";
//If the i-th day after the beginning of the start time is the end date, output a table with the background color #EE6363
}elseif(date("Ymd",$this->start+($i*(24*60*60)))==$this->end_year.$this->end_month.$this->end_day ){
$str.= "
";
//If the i-th day after the start of the month is the current date, output a table with the background color #EE2C2C
}elseif(date("ymd",$this->start+($i*(24*60*60)))==(date("ymd",time()))){
$str.= "
";
//Otherwise, output the table without background color
}else{
$str.= "
";
}
//Output the date of the i-th day after the start of the month
$str.= date("j",$this->start+($i*(24*60*60)));
$str.= "
";
//And change the row count counter to 1
$t=1;
}
//Return to end form
return $str;
}
/**
* Get the style and events of the output month table
* @return Returns the style and events of the month table
*/
private function get_style(){
$str = "";
$str .= "";
//Return file content
return $str;
}
/**
* Get date table
* @param i The i-th day after the start of the month, the default is 0
* @return Return date table
*/
public function get_datetab($i=0){
//Get styles and events
$str = $this->get_style();
//Start traversing dates based on the number of days from the start date to the end date
for($i=0,$t=1;$i<=$this->day_count;$i++){
//If it is the first day of this month
if(date("d",$this->start+($i*(24*60*60)))==1){
//Get header
$str.=$this->get_caption($i);
//Get the week header
$str.=$this->get_week($i);
}
//Complete the spaces in front of the table
$str.=$this->get_space_front($i);
//Get date
$str.=$this->get_date($i);
//Complete the spaces after the table
$str.=$this->get_space_back($i);
//Define the number of rows in the date table
$str.=$this->get_rows($i,$t);
//End form
$str.=$this->get_endtab($i,$t);
}
//Return date table
return $str;
}
}


I have added more complete comments. If you have anything you don’t understand, you can leave a reply and ask questions. Or I will explain the common problems that everyone encounters in detail in the next post. Please stay tuned....

Output effect:

I’ll post the code so you can download it and study it:

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

Author zdrjlamp

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478235.htmlTechArticleHello, everyone, I explained to you the basic idea of ​​RBAC [role-based access control] two days ago. , I don’t know if everyone understands its principle and whether it is easy to use,...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template