PHP完整的日历类(CLASS)
程序员文章站
2023-01-10 21:47:06
复制代码 代码如下:
<?php
class calendar{
/*
* 修正版
*/
var $year,$month,$day;
var $week=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
var $_month=array(
"01"=>"一月",
"02"=>"二月",
"03"=>"三月",
"04"=>"四月",
"05"=>"五月",
"06"=>"六月",
"07"=>"七月",
"08"=>"八月",
"09"=>"九月",
"10"=>"十月",
"11"=>"十一月",
"12"=>"十二月"
);
//设置年份
function setyear($year){
$this->year=$year;
}
//获得年份
function getyear(){
return $this->year;
}
//设置月份
function setmonth($month){
$this->month=$month;
}
//获得月份
function getmonth(){
return $this->month;
}
//设置日期
function setday($day){
$this->day=$day;
}
//获得日期
function getday(){
return $this->day;
}
//打印日历
function out(){
$this->_env();
$week=$this->getweek($this->year,$this->month,$this->day);//获得日期为星期几 (例如今天为2003-07-18,星期五)
$fweek=$this->getweek($this->year,$this->month,1); //获得此月第一天为星期几
echo "<div style=\"margin:0;border:1 solid black;width:300;font:9pt\"><form action=$_server[php_self] method=\"post\" style=\"margin:0\"><select name=\"month\" onchange=\"this.form.submit();\">";
for($ttmpa=1;$ttmpa<13;$ttmpa++){//打印12个月
$ttmpb=sprintf("%02d",$ttmpa);
if(strcmp($ttmpb,$this->month)==0){
$select="selected style=\"background-color:#c0c0c0\"";
}else{
$select="";
}
echo "<option value=\".$ttmpb\".$select>".$this->month[$ttmpb]."</option>\r\n";
}
echo " </select> <select name=\"year\" onchange=\"this.form.submit();\">";//打印年份,前后10年
for($ctmpa=$this->year-10;$ctmpa<$this->year+10;$ctmpa++){
if($ctmpa>2037){
break;
}
if($ctmpa<1970){
continue;
}
if(strcmp($ctmpa,$this->year)==0){
$select="selected style=\"background-color:#c0c0c0\"";
}else{
$select="";
}
echo "<option value=\"$ctmpa\" $select>$ctmpa</option>\r\n";
}
echo "</select>
</form>
<table border=0 align=center>";
for($tmpa=0;$tmpa<count($this->week);$tmpa++){//打印星期标头
echo "<td>".$this->week[$tmpa];
}
for($tmpb=1;$tmpb<=date("t",mktime(0,0,0,$this->month,$this->day,$this->year));$tmpb++){//打印所有日期
if(strcmp($tmpb,$this->day)==0){ //获得当前日期,做标记
$flag=" bgcolor='#ff0000'";
}else{
$flag=' bgcolor=#ffffff';
}
if($tmpb==1){
echo "<tr>"; //补充打印
for($tmpc=0;$tmpc<$fweek;$tmpc++){
echo "<td>";
}
}
if(strcmp($this->getweek($this->year,$this->month,$tmpb),0)==0){
echo "<tr><td align=center $flag>$tmpb";
}else{
echo "<td align=center $flag>$tmpb";
}
}
echo "</table></div>";
}
//获得方法内指定的日期的星期数
function getweek($year,$month,$day){
$week=date("w",mktime(0,0,0,$month,$day,$year));//获得星期
return $week;//获得星期
}
function _env(){
if(isset($_post["month"])){ //有指定月
$month=$_post["month"];
}else{
$month=date("m"); //默认为本月
}
if(isset($_post["year"])){ //有指年
$year=$_post["year"];
}else{
$year=date("y"); //默认为本年
}
$this->setyear($year);
$this->setmonth($month);
$this->setday(date("d"));
}
}
$d=new calendar;
$d->out();
?>
复制代码 代码如下:
<?php
class calendar{
/*
* 修正版
*/
var $year,$month,$day;
var $week=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
var $_month=array(
"01"=>"一月",
"02"=>"二月",
"03"=>"三月",
"04"=>"四月",
"05"=>"五月",
"06"=>"六月",
"07"=>"七月",
"08"=>"八月",
"09"=>"九月",
"10"=>"十月",
"11"=>"十一月",
"12"=>"十二月"
);
//设置年份
function setyear($year){
$this->year=$year;
}
//获得年份
function getyear(){
return $this->year;
}
//设置月份
function setmonth($month){
$this->month=$month;
}
//获得月份
function getmonth(){
return $this->month;
}
//设置日期
function setday($day){
$this->day=$day;
}
//获得日期
function getday(){
return $this->day;
}
//打印日历
function out(){
$this->_env();
$week=$this->getweek($this->year,$this->month,$this->day);//获得日期为星期几 (例如今天为2003-07-18,星期五)
$fweek=$this->getweek($this->year,$this->month,1); //获得此月第一天为星期几
echo "<div style=\"margin:0;border:1 solid black;width:300;font:9pt\"><form action=$_server[php_self] method=\"post\" style=\"margin:0\"><select name=\"month\" onchange=\"this.form.submit();\">";
for($ttmpa=1;$ttmpa<13;$ttmpa++){//打印12个月
$ttmpb=sprintf("%02d",$ttmpa);
if(strcmp($ttmpb,$this->month)==0){
$select="selected style=\"background-color:#c0c0c0\"";
}else{
$select="";
}
echo "<option value=\".$ttmpb\".$select>".$this->month[$ttmpb]."</option>\r\n";
}
echo " </select> <select name=\"year\" onchange=\"this.form.submit();\">";//打印年份,前后10年
for($ctmpa=$this->year-10;$ctmpa<$this->year+10;$ctmpa++){
if($ctmpa>2037){
break;
}
if($ctmpa<1970){
continue;
}
if(strcmp($ctmpa,$this->year)==0){
$select="selected style=\"background-color:#c0c0c0\"";
}else{
$select="";
}
echo "<option value=\"$ctmpa\" $select>$ctmpa</option>\r\n";
}
echo "</select>
</form>
<table border=0 align=center>";
for($tmpa=0;$tmpa<count($this->week);$tmpa++){//打印星期标头
echo "<td>".$this->week[$tmpa];
}
for($tmpb=1;$tmpb<=date("t",mktime(0,0,0,$this->month,$this->day,$this->year));$tmpb++){//打印所有日期
if(strcmp($tmpb,$this->day)==0){ //获得当前日期,做标记
$flag=" bgcolor='#ff0000'";
}else{
$flag=' bgcolor=#ffffff';
}
if($tmpb==1){
echo "<tr>"; //补充打印
for($tmpc=0;$tmpc<$fweek;$tmpc++){
echo "<td>";
}
}
if(strcmp($this->getweek($this->year,$this->month,$tmpb),0)==0){
echo "<tr><td align=center $flag>$tmpb";
}else{
echo "<td align=center $flag>$tmpb";
}
}
echo "</table></div>";
}
//获得方法内指定的日期的星期数
function getweek($year,$month,$day){
$week=date("w",mktime(0,0,0,$month,$day,$year));//获得星期
return $week;//获得星期
}
function _env(){
if(isset($_post["month"])){ //有指定月
$month=$_post["month"];
}else{
$month=date("m"); //默认为本月
}
if(isset($_post["year"])){ //有指年
$year=$_post["year"];
}else{
$year=date("y"); //默认为本年
}
$this->setyear($year);
$this->setmonth($month);
$this->setday(date("d"));
}
}
$d=new calendar;
$d->out();
?>
下一篇: 一个阿拉伯数字转中文数字的函数