PHP Simple Calendar Program code

Source: Internet
Author: User
Tags foreach vars

The date () function makes it easy to get the data above

The code is as follows Copy Code

<?php

$month = $_get[' m ']?$_get[' m ']:d ate (' n ');
$year = $_get[' y ']?$_get[' y ']:d ate (' Y ');

$start _week = Date (' W ', Mktime (0,0,0, $month, 1, $year));
$day _num = date (' t ', Mktime (0,0,0, $month, 1, $year));
$end = false;
?>
<table>
<tr>
<td> Sunday </td><td> Monday </td><td> Tuesday </td><td> Wednesday </td><td> Thursday </td><td> Friday </td><td> Saturday </td>
</tr>
<tr>
<?php
for ($i = 0; $i < $start _week; $i + +)
{
echo "<td></td>";
}

$j = 1;

while ($j <= $day _num)
{
echo "<td> $j </td>";
$week = ($start _week+ $j-1)%7;

if ($week ==6) {
echo "Nt</tr>n";
if ($j!= $day _num)
echo "T<tr>ntt";
else $end = true;
}
$j + +;
}
while ($week%7!= 6)
{
echo "<td></td>";
$week + +;
}
if (! $end)
echo "N</tr>";
?>

</table>

Advanced one-point class

The code is as follows Copy Code

<?php
Class Calendar
{
Private $year;
Private $month;
Private $weeks = Array (' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ');

function __construct ($options = Array ()) {
$this->year = Date (' Y ');
$this->month = Date (' m ');

$vars = Get_class_vars (Get_class ($this));
foreach ($options as $key => $value) {
if (Array_key_exists ($key, $vars)) {
$this-> $key = $value;
}
}
}

function display ()
{
echo ' <table class= calendar ' > ';
$this->showchangedate ();
$this->showweeks ();
$this->showdays ($this->year, $this->month);
Echo ' </table> ';
}

Private Function Showweeks ()
{
Echo ' <tr> ';
foreach ($this->weeks as $title)
{
Echo ' <th> '. $title. ' </th> ';
}
Echo ' </tr> ';
}

Private Function Showdays ($year, $month)
{
$firstDay = mktime (0, 0, 0, $month, 1, $year);
$starDay = Date (' W ', $firstDay);
$days = date (' t ', $firstDay);

Echo ' <tr> ';
for ($i =0; $i < $starDay; $i + +) {
Echo ' <td>&nbsp;</td> ';
}

for ($j =1; $j <= $days; $j + +) {
$i + +;
if ($j = = Date (' d ')) {
Echo ' <td class= ' Today > '. $j. ' </td> ';
} else {
Echo ' <td> '. $j. ' </td> ';
}
if ($i% 7 = 0) {
Echo ' </tr><tr> ';
}
}

Echo ' </tr> ';
}

Private Function Showchangedate ()
{

$url = basename ($_server[' php_self '));

Echo ' <tr> ';
Echo ' <td><a href= '. $this->preyearurl ($this->year, $this->month). " > '. ' << '. ' </a></td> ';
Echo ' <td><a href= '. $this->premonthurl ($this->year, $this->month). " > '. ' < '. ' </a></td> ';
Echo ' <td colspan= 3 ' ><form> ';

Echo ' <select name= ' year ' onchange= ' window.location= '. $url. Year= ' +this.options[selectedindex].value+ ' &month= '. $this->month. ' " > ';
For ($ye =1970 $ye <=2038; $ye + +) {
$selected = ($ye = = $this->year)? ' Selected ': ';
Echo ' <option '. $selected. ' value= '. $ye. ' " > '. $ye. ' </option> ';
}
Echo ' </select> ';
Echo ' <select name= ' month ' onchange= ' window.location= '. $url. ' Year= '. $this->year. ' &month= ' +this.options[selectedindex].value+ ' ">";


For ($mo =1 $mo <=12; $mo + +) {
$selected = ($mo = = $this->month)? ' Selected ': ';
Echo ' <option '. $selected. ' value= '. $mo. ' > ' $mo. ' </option> ';
}
Echo ' </select> ';
Echo ' </form></td> ';
Echo ' <td><a href= '. $this->nextmonthurl ($this->year, $this->month). " > '. ' > '. ' </a></td> ';
Echo ' <td><a href= '. $this->nextyearurl ($this->year, $this->month). " > '. ' >> '. ' </a></td> ';
Echo ' </tr> ';
}

Private Function Preyearurl ($year, $month)
{
$year = ($this->year <= 1970)? 1970: $year-1;

Return ' year= '. $year. ' &month= '. $month;
}

Private Function Nextyearurl ($year, $month)
{
$year = ($year >= 2038)? 2038: $year + 1;

Return ' year= '. $year. ' &month= '. $month;
}

Private Function Premonthurl ($year, $month)
{
if ($month = = 1) {
$month = 12;
$year = ($year <= 1970)? 1970: $year-1;
} else {
$month--;
}

Return ' year= '. $year. ' &month= '. $month;
}

Private Function Nextmonthurl ($year, $month)
{
if ($month = = 12) {
$month = 1;
$year = ($year >= 2038)? 2038: $year + 1;
}else{
$month + +;
}
Return ' year= '. $year. ' &month= '. $month;
}

}

Call method

The code is as follows Copy Code

<?php
$params = Array ();
if (Isset ($_get[' year ')) && isset ($_get[' month ')) {
$params = Array (
' Year ' => $_get[' year '],
' Month ' => $_get[' month '],
);
}
$params [' url '] = ' demo.php ';
Require_once ' calendar.class.php ';
?>

<title> Calendar demo</title>
<meta http-equiv= "Content-type" content= "text/html" charset= "UTF-8"/>
<style type= "Text/css" >
Table.calendar {
border:1px solid #050;
}
. calendar th,. calendar td {
width:30px;
Text-align:center;
}
. calendar th {
Background-color: #050;
Color: #fff;
}
. today{
Color: #fff;
Background-color: #050;
}
</style>
<body>
<div style= "Align:center" >
<?php
$cal = new Calendar ($params);
$cal->display ();
?>
</div>
</body>

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.