Go [PHP] [06-12] Making a simple calendar with PHP

Source: Internet
Author: User
Friends who often surf the web may see a beautiful calendar on someone's web page showing the year, month, and day, which is lovely. It is such a small calendar, but this page of other people's wonderful moving, become not monotonous. At this point we not only want to be able to put on our own web page also a, this small program is how to make. Actually making it is not very difficult, of course, there are many ways to make this calendar, but we are talking about PHP here, so we use PHP to make it.
To make it easier for everyone to understand the production, make a simple calendar here first.

This calendar program uses a large number of PHP date and time functions and judgment comparison statements, through the production of this small program allows us to have a general understanding of the date-time function.

For the simple program structure and the independence of the function block, I can use a large file can be made to complete the program into three separate small files, so that the function of each small file is relatively clear and centralized, which makes us easier to understand. These three files are index.php page layout file, gouzao.php construction calendar file, gouzaoconfig.php configuration calendar file. There is also a CSS cascading style sheet file.

Let's 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;}
?>







            

"> Backwards

"> Today

"> Forward


As can be seen from the above code, the index.php file mainly includes the STYLEDEFAULT.CSS style sheet file, gouzao.php constructs the calendar file. The function of this file is to organize all the files, either directly or indirectly, and then render them on the page according to the layout we envision. Simply put, this file plays a role in the layout.

gouzao.php

Require ("gouzaoconfig.php");
if (!isset ($month)) $month =$_request["Month"];
if (!isset ($year)) $year =$_request["year"];
$today =intval (Date ("D", Time ())), integer value of the//intval function output variable
if (! $month) $month =date ("M", Time ());//The Month $month number representation
if (! $year) $year =date ("Y", Time ()),//$year the year represented by a four-digit number
$day _count=1;
$month =intval ($month);//Gets the integer value of the $month variable
$MN = $mth [$month];//in gouzaoconfig.php $mth is an array, the uppercase month is obtained through the $mth array
$mn = $year. " Year ". $mn;//date of output calendar
$SD =date ("W", Mktime (0,0,0, $month, 0, $year));//The Day of the week, the number indicates that 0 means Sunday
$CD =1-$SD;
$nd =mktime (0,0,0, $month +1,0, $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 + +)//This determines the number of rows in the calendar
{
echo "\ n ";
for ($prow =1; $prow <8; $prow + +)//This determines the number of columns in the calendar
{
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 number of days for the calendar.

gouzaoconfig.php

Width of the calendar table
$tableWidth = 400;
$cellSpacing = 1;
$cellPadding = 0;

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

Depositing months into 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 chronology grid 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: Song body;
font-size:20px;
Font-weight:bold;
Color: #252216;
}

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

. daynamestext {
Font-family: Song body;
font-size:16px;
Font-weight:bold;
Color: #433D27;
Background-image:url (daybg.gif);
}

. rows {
Font-family: Song body;
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 with no date
. S20 {
Background-image:url ("Cdef5.gif");
Background-color: #EBEBEB;
}

Here a complete small calendar program has been completed. The operating environment of this applet requires a PHP4.1.0 version or higher. These four files need to be placed in the same file directory.

This is just a simple calendar program, more complex calendar programs we can use the database or file to store the corresponding data. This allows for advanced editing of the calendar by manipulating 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

  • Related Article

    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.