Home >Backend Development >PHP Tutorial >A brief discussion on the eighth bullet of PHP---Using PHP's date function to output calendar_PHP tutorial
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.= "
"; //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.= " | ";|
"; |
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