[PHP] [06-12] using PHP to create a simple calendar

Source: Internet
Author: User
[Go to] [PHP] [06-12] using PHP to create a simple calendar, a friend who frequently browses the webpage on the Internet can see a beautiful calendar on someone else's webpage showing the year and month, day, very cute. It is such a small calendar, but the webpage of others is so fascinating that it is not monotonous. At this time, we not only need to think about whether we can add one on our own web pages, but also how this mini-program is made. In fact, it is not very difficult to create it. Of course there are many ways to create this calendar, but we are talking about PHP here, so we will use PHP to make it here.
In order to make it easier for everyone to understand how to create a calendar, create a simple calendar here.

This calendar program uses a lot of PHP date and time functions and comparison statements. by making this applet, we can have a rough understanding of the date and time functions.

To simplify the program structure and independence of functional blocks, I divided a program that can be created with a large file into three independent small files, in this way, the functions of each small file are clear and concentrated, which makes it easier to understand. These three files are respectively the index. php page layout file, gouzao. php to construct the calendar file, and gouzaoconfig. php to configure the calendar file. There is also a CSS stacked style sheet file.

Next let's take a look at the index. php file:



Simple calendar





Require ("gouzao. php ");
?>
$ Yb = $ year;
$ Yf = $ year;
$ Mb = $ month-1;
If ($ mb <1) {$ mb = 12; $ yb = $ year-1 ;}
$ Mf = $ month + 1;
If ($ mf> 12) {$ mf = 1; $ yf = $ year + 1 ;}
?>







            

"> Backward

"> Today

"> Forward


You can see that the index.php file includes the styledefault.css style table file and gouzao. php file to construct the calendar file. The function of this file is to organize all files directly or indirectly, and then present them on the page according to the layout we imagined. Simply put, this file serves as a layout.

Gouzao. php

Require ("gouzaoConfig. php ");
If (! Isset ($ month) $ month = $ _ REQUEST ["month"];
If (! Isset ($ year) $ year = $ _ REQUEST ["year"];
$ Today = intval (date ("d", time (); // The integer of the output variable of the intval function
If (! $ Month) $ month = date ("m", time (); // The month represented by the $ month number
If (! $ Year) $ year = date ("Y", time (); // The year represented by four digits
$ Day_count = 1;
$ Month = intval ($ month); // gets the integer of the $ month variable.
$ Mn = $ mth [$ month]; // $ mth is an array in gouzaoConfig. php, and an upper-case month is obtained through the $ mth array.
$ Mn = $ year. "year". $ mn; // output the calendar year and month.
$ Sd = date ("w", mktime (, 0, 0, $ month, 0, $ year); // The Day of the week. a number indicates that the value 0 indicates that the day of the week is Sunday.
$ Cd = 1-$ sd;
$ Nd = mktime (, 0, $ month +, $ year );
$ Nd = (strftime ("% d", $ nd) + 1;
Echo"

BORDER = 0> \ n ";
Echo"\ N ";
Echo"\ N $ mn \ n\ N ";
Echo"\ N ";/**/
Echo"\ N ";
For ($ I = 1; $ I <= 7; $ I ++)
{
$ Dayprint = $ I;
If ($ dayprint = 7) $ dayprint = $ dayprint-7;
Echo"$ Day [$ dayprint]\ N ";
}
Echo"\ N ";/**/
For ($ I = 1; $ I <7; $ I ++) // specifies the number of calendar rows.
{
Echo"\ N ";
For ($ prow = 1; $ prow <8; $ prow ++) // specifies the number of calendar columns.
{
If ($ day_count ==$ today & $ highlightToday = 1 & $ cd> 0 & $ cd <$ nd)
{
Echo"

Echo "> $ cd\ N ";
$ Day_count ++;
$ Cd ++;
}
Else
{
If ($ cd> 0 & $ cd <$ nd)
{
Echo"Echo "> $ cd\ N ";
$ Day_count ++;
}
Else
{
Echo" \ N ";
}
$ Cd ++;
}
}
Echo"\ N ";
}
Echo"\ N ";
?>

This code mainly uses two for loop statements to output the correct days of the calendar.

Gouzaoconfig. php

// The width of the calendar table
$ TableWidth = 400;
$ CellSpacing = 1;
$ CellPadding = 0;

// Save the week to the array
$ Day [0] = "Sunday ";
$ Day [1] = "Monday ";
$ Day [2] = "Tuesday ";
$ Day [3] = "Wednesday ";
$ Day [4] = "Thursday ";
$ Day [5] = "Friday ";
$ Day [6] = "Saturday ";

// Save the month to an array
$ Mth [1] = "January ";
$ Mth [2] = "February ";
$ Mth [3] = "March ";
$ Mth [4] = "April ";
$ Mth [5] = "May ";
$ Mth [6] = "June ";
$ Mth [7] = "July ";
$ Mth [8] = "August ";
$ Mth [9] = "September ";
$ Mth [10] = "October ";
$ Mth [11] = "November ";
$ Mth [12] = "December ";
?>

Styledefault.css

. MainTable {
Background-color: # FFFFFF;
Border: 1px solid #003366;
}

/////////// Month and year table row settings
. MonthYearRow {
Line-height: 17pt;
Background-color: # FFFFFF;
Text-align: center;
Vertical-align: middle;
Background-image: url(monthBg.gif );
}
////////// Month and year text settings
. MonthYearText {
Font-family:;
Font-size: 20px;
Font-weight: Bold;
Color: #252216;
}

. DayNamesRow {
Line-height: 13pt;
Background-color: # F5F4D3;
Text-align: center;
Vertical-align: middle;
}

. DayNamesText {
Font-family:;
Font-size: 16px;
Font-weight: Bold;
Color: #433D27;
Background-image: url(dayBg.gif );
}

. Rows {
Font-family:;
Font-size: 12px;
Color: #433D27;
Line-height: 25pt;
Text-align: center;
Vertical-align: middle;
}

///////// Today's date color
. Today {
Color: # CF0000;
Font-weight: Bold;
Font-size: 16px;
}

//////// Cell background with date
. S2 {
Background-image: url ("cdef.gif ");
Background-color: # EEEEEE;
}
/////// Cell background without date
. S20 {
Background-image: url ("cdef5.gif ");
Background-color: # EBEBEB;
}

A complete small calendar program has been created. The runtime environment of this applet requires PHP4.1.0 or later. These four files must be placed in the same file directory.

This is just a simple calendar program. for more complex calendar programs, we can use databases or files to store the corresponding data. In this way, you can perform advanced editing on the calendar by operating the database or file.

This article from: 171 webmaster forum (http://www.171zz.com/) detailed article reference: http://www.171zz.com/thread-56358-1-1.html

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.