PHP Simple calendar function _php Tips

Source: Internet
Author: User

The example in this article describes the calendar program that PHP implements. Share to everyone for your reference. as follows:

<?php * * PHP Output Calendar program/header ("Content-type:text/html;charset=utf-8"); $year = (!isset ($_get[' year ')) | | 
$_get[' year ']== ")" Date ("Y"): $_get[' year ']; $month = (!isset ($_get[' month ')) | | 
$_get[' month ']== "") Date ("n"): $_get[' month ']; 
 
$curUrl =$_server[' php_self ']; 
  if ($year <1971) {echo "Error!"; 
  echo "<BR>"; 
  echo "<a href=", $curUrl, ">Back</a>"; 
Exit (); ?> <table width= "border=" 1 "cellspacing=" 0 "cellpadding=" 0 "bordercolor=" #E7E7E7 "style=" FONT-SIZE:12PX; " align= "center" > <tr align= "center" ><td colspan= "2" > <?php//<-------The month is more than 1 to 12 o'clock of processing; start------- 
  > if ($month <1) {$month = 12; 
$year-=1; 
  } if ($month >12) {$month = 1; 
$year +=1; //<-------processing over 1 to 12 o'clock in the month;------->//<---------last year, next year, last month, next month's connection processing and output; start---------> echo "<a href=", $CURURL, "year=". ($year-1). " &month= ". $month." ><<</a> year <a href= ", $curUrl,"? year= ". ($year + 1). " &month= ". $month." >>></a> "; ?> </td><td colspan= "3" ><?php echo $year. " Year ". $month." Month ";? > </td><td colspan= "2" > <?php echo "<a href=", $curUrl, "? month=". ($month-1). " &year= ". $year." ><<</a> month <a href= ", $curUrl,"? month= ". ($month + 1). " &year= ". $year." 
>>></a> "; <--------last year, next year, last month, next month's connection processing and output; end--------->?></td></tr> <tr align=center><td> <font color= "Red" > Day </font></td><td> one </td><td> two </td><td> three </td ><td> four </td><td> five </td><td> six </td></tr><tr> <?php $d =date ("D" 
); 
$FirstDay =date ("W", Mktime (0,0,0, $month, 1, $year))//Get any one-month-a number is the day of the week, used to calculate a number is by the table in the first few grid $bgtoday =date ("D"); 
  function Font_color ($month, $today, $year) {//used to calculate Sunday font color $sunday =date ("W", Mktime (0,0,0, $month, $today, $year)); 
  if ($sunday = = "0") {$FontColor = "red"; 
  }else{$FontColor = "BLACK"; 
return $FontColor; } function bgcolor ($moNth, $bgtoday, $today _i, $year) {//used to calculate the background color of the day $show _today=date ("D", Mktime (0,0,0, $month, $today _i, $year)); 
  $sys _today=date ("D", Mktime (0,0,0, $month, $bgtoday, $year)); 
  if ($show _today== $sys _today) {$bgcolor = "bgcolor= #6699FF"; 
  }else{$bgcolor = ""; 
return $bgcolor; 
  function Font_style ($month, $today, $year) {//used to calculate Sunday font style $sunday =date ("W", Mktime (0,0,0, $month, $today, $year)); 
  if ($sunday = = "0") {$FontStyle = "<strong>"; 
  }else{$FontStyle = ""; 
return $FontStyle; For ($i =0 $i <= $FirstDay; $i + +) {//this for outputs a position for a month for ($i; $i < $FirstDay; $i + +) {echo "<td align=center> 
  </td>\n "; } if ($i = = $FirstDay) {echo "<td align=center". bgcolor ($month, $bgtoday, 1, $year). " ><font color= ". Font_color ($month, 1, $year)." > ". Font_style ($month, 1, $year)." 
    1</font></td>\n "; 
    if ($FirstDay ==6) {//Judge whether number 1th is Saturday echo "</tr>"; }} $countMonth =date ("T", Mktime (0,0,0, $month, 1, $year));//Total number of days of the month for ($i =2$i <= $countMonth $i + +) {//output is positioned by number 1th, followed by the echo "<td align=center" of all numbers up to the end of the month. bgcolor ($month, $bgtoday, $i, $year). " ><font color= ". Font_color ($month, $i, $year)." > ". Font_style ($month, $i, $year)." 
  $i </font></td>\n "; 
  if (Date ("W", Mktime (0,0,0, $month, $i, $year)) ==6) {//Judge whether the day is Saturday echo "</tr>\n"; }}?> </table>

The calendar effect of the PHP output is as follows:

I hope this article is helpful for you to learn PHP program design.

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.