用PHP与AJAX实现的一个全年历
时间:2008-09-22
来源:互联网
由于选择了自己的博客使用原创,所以当然博客的每个功能都要自己亲自写了,现在首先介绍的是我的博客日历,自己写了一个简单的类,只需填写简单的年,月即可生成你想要的某一年、某一月的日历数据,当然也可以添加NEXT功能,看你怎样用了,用起来自己觉得也方便,因为是自己原创的东西,故与大家共享一下:核心是两个文件,一个是JS,一个是PHP日历类。博客地址:http://www.fla8d.cn/pblog
1、ajax.js
[code]var xmlhttp=false;
try{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
//alert("You are using Microsoft Internet Explorer.");
}
catch(e){
try{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
//alert("You are using Microsoft Internet Explorer");
}catch(e){
xmlhttp=false;
}
}
if(!xmlhttp&&typeof XMLHttpRequest!='undefined'){
xmlhttp=new XMLHttpRequest();
//alert("You are not using Microsoft Internet Explorer");
}
function makerequest(serverPage,objID){
//xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
var obj=document.getElementById(objID);
xmlhttp.open("GET",serverPage,false);
//xmlhttp.setRequestHeader('Content-type','text/html;charset=gb2312;');
xmlhttp.setRequestHeader('Content-type',
'application/x-www-form-urlencoded;charset=gbk;');
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
obj.innerHTML=xmlhttp.responseText;
}
else{
obj.innerHTML="载入中........";
}
}
xmlhttp.send(null);
}
var showCalendar=true;
function showHideCalendar(){
var objID="calendar";
if(showCalendar==true){
document.getElementById("opencloseimg").src = "mins.gif";
var serverpage="calendar.php";
showCalendar=false;
var obj=document.getElementById(objID);
xmlhttp.open("GET",serverpage);
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
obj.innerHTML=xmlhttp.responseText;
}else{
obj.innerHTML="loading.....";
}
}
xmlhttp.send(null);
}else{
document.getElementById("opencloseimg").src="plus.gif";
showCalendar=true;
document.getElementById(objID).innerHTML='';
}
}[/code]
2、自己写的日历类(calendar.class.php)
<?php
class calendar{
var $dates;//一个月总共有多少天
var $fir_date;//一个月的开始是星期几
var $month;//哪个月
var $dj_date;//一个月中的第几天
var $year;//哪一年
function __construct($year,$month){
//实例化时自动调用构造函数
$this->year = $year;
$this->month=$month;
$this->dates=date("t",mktime(0,0,0,$this->month,1,$this->year));
$this->fir_date=date("N",mktime(0,0,0,$this->month,1,$this->year));
//$dj_date=date('j');
$this->dj_date=date("j");
date_default_timezone_set('prc');//设置时区为中国时间
}
function __destruct(){
//利用析构函数用来销毁对象,释放内存,这个函数会自动调用
}
function build_calendar(){
$rows=ceil(($this->dates)/7);//取整操作
$date=$this->year."-".$this->month;
echo "<div style='float:left;'><table style='text-align:center;color:red;font-size:16px;width:250px;border-collapse:'border=1';' bordercolor=#27a7e7 cellspacing=0>
<tr><td><b>$date</b></td></tr></table><table style='text-align:center;font-size:14px;width:250px;border-collapse:'border=1';' bordercolor=#27a7e7 cellspacing=0><tr><td>Sun</td><td>Mon</td><td>Tue</td>
<td>Wed</td><td>Thu</td><td>Fri</td><td>Sat</td></tr>";
echo "<tr>";
static $h;//创建静态变量,保存日期数
//echo $this->fir_date;
if($this->fir_date!=7){
//如果一个月的开始不是星期日,则用空表格填充
for($k=1;$k<=($this->fir_date);$k++){
echo "<td> </td>";
}
for($h=1;$h<=((7-$this->fir_date));$h++){
if(($h==$this->dj_date) && ($this->year == date("Y") && $this->month == date("n"))){
//如果日期为当前日期,则加亮显示
echo "<td style='background:#27a7e7;' >$h</td>";
}else{
echo "<td onmouseover=this.style.background='#27a7e7' onmouseout=this.style.background='' >$h</td>";
}
}
}
else{
for($h=1;$h<=7;$h++){
if(($h==$this->dj_date) && ($this->year == date("Y") && $this->month == date("n"))){
echo "<td style='background:#27a7e7;' >$h</td>";
}else{
echo "<td onmouseover=this.style.background='#27a7e7' onmouseout=this.style.background='' >$h</td>";
}
}
}
$h--;
echo "</tr>";
for($i=2;$i<=$rows;$i++){
echo "<tr>";
for($j=1;$j<=7;$j++){
++$h;
if($h>$this->dates){
echo "<td> </td>";
}else{
if(($h==$this->dj_date) && ($this->year == date("Y") && $this->month == date("n"))){
echo "<td style='background:#27a7e7;' >$h</td>";
}else{
echo "<td onmouseover=this.style.background='#27a7e7' onmouseout=this.style.background='' >$h</td>";
}
}
}
echo "</tr>";
}
echo "<tr>";
for($j=1;$j<=7;$j++){
++$h;
if($h>($this->dates)){
//echo $h;
echo "<td> </td>";
}else{
if(($h==$this->dj_date) && ($this->year == date("Y") && $this->month == date("n"))){
echo "<td style='background:#27a7e7;'>$h</td>";
}else{
echo "<td onmouseover=this.style.background='#27a7e7' onmouseout=this.style.background='' >$h</td>";
}
}
}
echo "</tr></table></div>";
//echo $rows;
unset($h);//销毁静态变量
}
}
?>
下面就是实例化调用过程了,由于代码写的太多了所以上传附件供大家参考,指正。演示地址:http://www.fla8d.cn/pblog/?id=14&style=blue&typeid=1011
1、ajax.js
[code]var xmlhttp=false;
try{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
//alert("You are using Microsoft Internet Explorer.");
}
catch(e){
try{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
//alert("You are using Microsoft Internet Explorer");
}catch(e){
xmlhttp=false;
}
}
if(!xmlhttp&&typeof XMLHttpRequest!='undefined'){
xmlhttp=new XMLHttpRequest();
//alert("You are not using Microsoft Internet Explorer");
}
function makerequest(serverPage,objID){
//xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
var obj=document.getElementById(objID);
xmlhttp.open("GET",serverPage,false);
//xmlhttp.setRequestHeader('Content-type','text/html;charset=gb2312;');
xmlhttp.setRequestHeader('Content-type',
'application/x-www-form-urlencoded;charset=gbk;');
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
obj.innerHTML=xmlhttp.responseText;
}
else{
obj.innerHTML="载入中........";
}
}
xmlhttp.send(null);
}
var showCalendar=true;
function showHideCalendar(){
var objID="calendar";
if(showCalendar==true){
document.getElementById("opencloseimg").src = "mins.gif";
var serverpage="calendar.php";
showCalendar=false;
var obj=document.getElementById(objID);
xmlhttp.open("GET",serverpage);
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
obj.innerHTML=xmlhttp.responseText;
}else{
obj.innerHTML="loading.....";
}
}
xmlhttp.send(null);
}else{
document.getElementById("opencloseimg").src="plus.gif";
showCalendar=true;
document.getElementById(objID).innerHTML='';
}
}[/code]
2、自己写的日历类(calendar.class.php)
<?php
class calendar{
var $dates;//一个月总共有多少天
var $fir_date;//一个月的开始是星期几
var $month;//哪个月
var $dj_date;//一个月中的第几天
var $year;//哪一年
function __construct($year,$month){
//实例化时自动调用构造函数
$this->year = $year;
$this->month=$month;
$this->dates=date("t",mktime(0,0,0,$this->month,1,$this->year));
$this->fir_date=date("N",mktime(0,0,0,$this->month,1,$this->year));
//$dj_date=date('j');
$this->dj_date=date("j");
date_default_timezone_set('prc');//设置时区为中国时间
}
function __destruct(){
//利用析构函数用来销毁对象,释放内存,这个函数会自动调用
}
function build_calendar(){
$rows=ceil(($this->dates)/7);//取整操作
$date=$this->year."-".$this->month;
echo "<div style='float:left;'><table style='text-align:center;color:red;font-size:16px;width:250px;border-collapse:'border=1';' bordercolor=#27a7e7 cellspacing=0>
<tr><td><b>$date</b></td></tr></table><table style='text-align:center;font-size:14px;width:250px;border-collapse:'border=1';' bordercolor=#27a7e7 cellspacing=0><tr><td>Sun</td><td>Mon</td><td>Tue</td>
<td>Wed</td><td>Thu</td><td>Fri</td><td>Sat</td></tr>";
echo "<tr>";
static $h;//创建静态变量,保存日期数
//echo $this->fir_date;
if($this->fir_date!=7){
//如果一个月的开始不是星期日,则用空表格填充
for($k=1;$k<=($this->fir_date);$k++){
echo "<td> </td>";
}
for($h=1;$h<=((7-$this->fir_date));$h++){
if(($h==$this->dj_date) && ($this->year == date("Y") && $this->month == date("n"))){
//如果日期为当前日期,则加亮显示
echo "<td style='background:#27a7e7;' >$h</td>";
}else{
echo "<td onmouseover=this.style.background='#27a7e7' onmouseout=this.style.background='' >$h</td>";
}
}
}
else{
for($h=1;$h<=7;$h++){
if(($h==$this->dj_date) && ($this->year == date("Y") && $this->month == date("n"))){
echo "<td style='background:#27a7e7;' >$h</td>";
}else{
echo "<td onmouseover=this.style.background='#27a7e7' onmouseout=this.style.background='' >$h</td>";
}
}
}
$h--;
echo "</tr>";
for($i=2;$i<=$rows;$i++){
echo "<tr>";
for($j=1;$j<=7;$j++){
++$h;
if($h>$this->dates){
echo "<td> </td>";
}else{
if(($h==$this->dj_date) && ($this->year == date("Y") && $this->month == date("n"))){
echo "<td style='background:#27a7e7;' >$h</td>";
}else{
echo "<td onmouseover=this.style.background='#27a7e7' onmouseout=this.style.background='' >$h</td>";
}
}
}
echo "</tr>";
}
echo "<tr>";
for($j=1;$j<=7;$j++){
++$h;
if($h>($this->dates)){
//echo $h;
echo "<td> </td>";
}else{
if(($h==$this->dj_date) && ($this->year == date("Y") && $this->month == date("n"))){
echo "<td style='background:#27a7e7;'>$h</td>";
}else{
echo "<td onmouseover=this.style.background='#27a7e7' onmouseout=this.style.background='' >$h</td>";
}
}
}
echo "</tr></table></div>";
//echo $rows;
unset($h);//销毁静态变量
}
}
?>
下面就是实例化调用过程了,由于代码写的太多了所以上传附件供大家参考,指正。演示地址:http://www.fla8d.cn/pblog/?id=14&style=blue&typeid=1011

example.rar (2.75 KB)
作者: pangjincai 发布时间: 2008-09-22
下载看了。很好。^_^
不过运行时出现了一点问题:
celendar.class.php中函数date_default_timezone_set未定义。
注释掉这行就运行正常了。
不过运行时出现了一点问题:
celendar.class.php中函数date_default_timezone_set未定义。
注释掉这行就运行正常了。
作者: mingpi 发布时间: 2008-09-23
不错,支持鼓励一下原创!!!
作者: fly1983 发布时间: 2008-09-23
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28