php日历[测试通过]


Posted in PHP onMarch 27, 2008

比较不错的一款php日历代码

<?php 
/** 
* 日历 
* 
* Copyright(c) 2007 by 陈毅鑫(深空). All rights reserved 
* To contact the author write to {@link mailto:shenkong@php.net} 
* @author 陈毅鑫(深空) 
*/ 
if (function_exists('date_default_timezone_set')) { 
date_default_timezone_set('Asia/Chongqing'); 
} 
$date = isset($_GET['date']) ? $_GET['date'] : date('Y-m-d'); 
$date = getdate(strtotime($date)); 
$end = getdate(mktime(0, 0, 0, $date['mon'] + 1, 1, $date['year']) - 1); 
$start = getdate(mktime(0, 0, 0, $date['mon'], 1, $date['year'])); 
$pre = date('Y-m-d', $start[0] - 1); 
$next = date('Y-m-d', $end[0] + 86400); 
$html = '<table border="1">'; 
$html .= '<tr>'; 
$html .= '<td><a href="' . $PHP_SELF . '?date=' . $pre . '">-</a></td>'; 
$html .= '<td colspan="5">' . $date['year'] . ';' . $date['month'] . '</td>'; 
$html .= '<td><a href="' . $PHP_SELF . '?date=' . $next . '">+</a></td>'; 
$html .= '</tr>'; 
$arr_tpl = array(0 => '', 1 => '', 2 => '', 3 => '', 4 => '', 5 => '', 6 => ''); 
$date_arr = array(); 
$j = 0; 
for ($i = 0; $i < $end['mday']; $i++) { 
if (!isset($date_arr[$j])) { 
$date_arr[$j] = $arr_tpl; 
} 
$date_arr[$j][($i+$start['wday'])%7] = $i+1; 
if ($date_arr[$j][6]) { 
$j++; 
} 
} 
foreach ($date_arr as $value) { 
$html .= '<tr>'; 
foreach ($value as $v) { 
if ($v) { 
if ($v == $date['mday']) { 
$html .= '<td><b>' . $v . '</b></td>'; 
} else { 
$html .= '<td>' . $v . '</td>'; 
} 
} else { 
$html .= '<td> </td>'; 
} 
} 
$html .= '</tr>'; 
} 
$html .= '</table>'; 
echo $html; 
?>

php日历代码2
<?php 
/** 
* 日历 
*/ 
if (function_exists('date_default_timezone_set')) { 
date_default_timezone_set('Asia/Chongqing'); 
} 
$date = isset($_GET['date']) ? $_GET['date'] : date('Y-m-d'); 
$date = getdate(strtotime($date)); 
$end = getdate(mktime(0, 0, 0, $date['mon'] + 1, 1, $date['year']) - 1); 
$start = getdate(mktime(0, 0, 0, $date['mon'], 1, $date['year'])); 
$pre = date('Y-m-d', $start[0] - 1); 
$next = date('Y-m-d', $end[0] + 86400); 
$html = '<table width="200" border="1" cellspacing="0" bordercolor="#999999" 
align="center" style="line-height:150%; font-family:Verdana,宋体; font-size: 12px;">'; 
$html .= '<tr>'; 
$html .= '<td><a href="' . $PHP_SELF . '?date=' . $pre . '">-</a></td>'; 
$html .= '<td colspan="5">' . $date['year'] . ';' . $date['month'] . '</td>'; 
$html .= '<td><a href="' . $PHP_SELF . '?date=' . $next . '">+</a></td>'; 
$html .= '</tr>'; 
$arr_tpl = array(0 => '', 1 => '', 2 => '', 3 => '', 4 => '', 5 => '', 6 => ''); 
$date_arr = array(); 
$j = 0; 
for ($i = 0; $i < $end['mday']; $i++) { 
if (!isset($date_arr[$j])) { 
$date_arr[$j] = $arr_tpl; 
} 
$date_arr[$j][($i+$start['wday'])%7] = $i+1; 
if ($date_arr[$j][6]) { 
$j++; 
} 
} 
foreach ($date_arr as $value) { 
$html .= '<tr>'; 
foreach ($value as $v) { 
if ($v) { 
if ($v == $date['mday']) { 
$html .= '<td><b>' . $v . '</b></td>'; 
} else { 
$html .= '<td>' . $v . '</td>'; 
} 
} else { 
$html .= '<td> </td>'; 
} 
} 
$html .= '</tr>'; 
} 
$html .= '</table>'; 
echo $html; 
?>

下面这个也不错,提示有错误,思路清晰
<?php 
function calendar() 
{ 
    if($_GET['ym']) 
    { 
        $year = substr($_GET['ym'],0,4); 
        $month = substr($_GET['ym'],4,(strlen($_GET['ym'])-4));         if($month>12) 
        { 
            $year += floor($month/12); 
            $month = $month % 12; 
        } 
        if($year > 2030) $year = 2030; 
        if($year < 1980) $year = 1980; 
    } 
    $year = isset($year) ? $year : date('Y'); 
    $month = isset($month) ? $month : date('n'); 
    if($year==date('Y') && $month==date('n')) $today = date('j'); 
    if($month-1 == 0) 
        $prevmonth = ($year - 1)."12"; 
    else $prevmonth = $year.($month - 1); 
    if($month+1 == 13) 
        $nextmonth = ($year+1)."1"; 
    else $nextmonth = $year.($month+1); 
    $prevyear = ($year - 1).$month; 
    $nextyear = ($year + 1).$month; 
    echo <<<VKN 
        <table width="200" border="0" cellpadding="2" cellspacing="2"> 
  <tr> 
    <td class="weekday"><a href="?ym=$prevyear"><<</a></td> 
    <td class="normalday"><a href="?ym=$prevmonth"><</a></td> 
    <td colspan="3" class="normalday">$year - $month</td> 
    <td class="normalday"><a href="?ym=$nextmonth">></a></td> 
    <td class="weekday"><a href="?ym=$nextyear">>></a></td> 
  </tr> 
  <tr> 
    <td width="27" class="weekday">日</td> 
    <td width="27" class="normalday">一</td> 
    <td width="27" class="normalday">二</td> 
    <td width="27" class="normalday">三</td> 
    <td width="27" class="normalday">四</td> 
    <td width="27" class="normalday">五</td> 
    <td width="27" class="weekday">六</td> 
  </tr> 
VKN; 
    $nowtime = mktime(0,0,0,$month,1,$year);//当月1号转为秒 
    $daysofmonth = date(t,$nowtime);//当月天数 
    $weekofbeginday = date(w,$nowtime);//当月第一天是星期几 
    $weekofendday = date(w,mktime(0,0,0,$month+1,0,$year));//当月最后一天是星期几 
    $daysofprevmonth = date(t,mktime(0,0,0,$month,0,$year));//上个月天数 
    $count = 1;//计数 
    //列出上月后几天 
    for($i = 1 ; $i <= $weekofbeginday ; $i++) 
        { 
            echo     "<td class='othermonth'>".($daysofprevmonth-$weekofbeginday+$i)."</td>"; 
            $count++; 
        } 
    //当月全部 
    for($i = 1 ; $i <= $daysofmonth ; $i++) 
        { 
            $css = ($count%7==0 || $count%7==1)?"weekday":"normalday"; 
            if($i == $today) $css .= "today"; 
            echo     "<td class='".$css."'>".$i."</td>"; 
            if($count%7==0) echo "</tr><tr>"; 
            $count++; 
        } 
    //下月前几天 
    for ($i = 1;$i <= 6-$weekofendday;$i++) 
        { 
            echo     "<td class='othermonth'>".$i."</td>"; 
        } 
    echo <<<VKN 
          <tr> 
    <td colspan="7"></td> 
  </tr> 
</table> 
VKN; 
} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>calendar</title> 
<style type="text/css"> 
<!-- 
.weekday { 
    font-size: 9pt; 
    color: #FF0000; 
    text-align: center; 
} 
.normalday { 
    font-size: 9pt; 
    color: #000000; 
    text-align: center; 
} 
.weekdaytoday { 
    font-size: 9pt; 
    color: #FF0000; 
    text-align: center; 
    background-color: #FFD9D9; 
    font-weight: bold; 
} 
.normaldaytoday { 
    font-size: 9pt; 
    color: #000000; 
    text-align: center; 
    background-color: #DDDDDD; 
    font-weight: bold; 
} 
.othermonth { 
    font-size: 9pt; 
    font-style: italic; 
    color: #999999; 
    text-align: center; 
} 
--> 
</style> 
</head> 
<body> 
<?php calendar();?> 
</body> 
</html>
PHP 相关文章推荐
PHP加速 eAccelerator配置和使用指南
Jun 05 PHP
PHP与MySQL开发的8个技巧小结
Dec 17 PHP
PHP学习笔记 IIS7下安装配置php环境
Oct 29 PHP
基于php socket(fsockopen)的应用实例分析
Jun 02 PHP
PHP中cookie和session的区别实例分析
Aug 28 PHP
php实现无限级分类(递归方法)
Aug 06 PHP
thinkphp框架下实现登录、注册、找回密码功能
Apr 06 PHP
Yii使用DeleteAll连表删除出现报错问题的解决方法
Jul 14 PHP
详解PHP归并排序的实现
Oct 18 PHP
详解yii2使用多个数据库的案例
Jun 16 PHP
PHP给前端返回一个JSON对象的实例讲解
May 31 PHP
Laravel 类和接口注入相关的代码
Oct 15 PHP
PHP与MySQL开发中页面乱码的产生与解决
Mar 27 #PHP
php中cookie的作用域
Mar 27 #PHP
简单的PHP图片上传程序
Mar 27 #PHP
php中变量及部分适用方法
Mar 27 #PHP
php Undefined index和Undefined variable的解决方法
Mar 27 #PHP
php.ini中的php-5.2.0配置指令详解
Mar 27 #PHP
一家之言的经验之谈php+mysql扎实个人基本功
Mar 27 #PHP
You might like
php5.2时间相差8小时
2007/01/15 PHP
一些使用频率比较高的php函数
2008/10/03 PHP
PHP中使用crypt()实现用户身份验证的代码
2012/09/05 PHP
ThinkPHP实现跨模块调用操作方法概述
2014/06/20 PHP
PHP-CGI远程代码执行漏洞分析与防范
2017/05/07 PHP
JQuery,Extjs,YUI,Prototype,Dojo 等JS框架的区别和应用场景简述
2010/04/15 Javascript
jquery.post用法关于type设置问题补充
2014/01/03 Javascript
使用GruntJS构建Web程序之安装篇
2014/06/04 Javascript
js获取IP地址的方法小结
2014/07/01 Javascript
javascript获取flash版本号的方法
2014/11/20 Javascript
js实现非常简单的焦点图切换特效实例
2015/05/07 Javascript
使用JavaScript和CSS实现文本隔行换色的方法
2015/11/04 Javascript
Fullpage.js固定导航栏-实现定位导航栏
2016/03/17 Javascript
基于Node.js的JavaScript项目构建工具gulp的使用教程
2016/05/20 Javascript
AngularJS实现单独作用域内的数据操作
2016/09/05 Javascript
vue2.0实现倒计时的插件(时间戳 刷新 跳转 都不影响)
2017/03/30 Javascript
Node错误处理笔记之挖坑系列教程
2018/06/05 Javascript
Node.js一行代码实现静态文件服务器的方法步骤
2019/05/07 Javascript
利用d3.js实现蜂巢图表带动画效果
2019/09/03 Javascript
vue实现简单加法计算器
2020/10/22 Javascript
Python程序员面试题 你必须提前准备!
2018/01/16 Python
python基础教程项目五之虚拟茶话会
2018/04/02 Python
libreoffice python 操作word及excel文档的方法
2019/07/04 Python
Python如何使用队列方式实现多线程爬虫
2020/05/12 Python
Java Unsafe类实现原理及测试代码
2020/09/15 Python
4款Python 类型检查工具,你选择哪个呢?
2020/10/30 Python
微信小程序之html5 canvas绘图并保存到系统相册
2019/06/20 HTML / CSS
Speedo速比涛中国官方网站:全球领先泳装运动品牌
2018/04/24 全球购物
linux系统都有哪些运行级别
2016/03/26 面试题
安全教育月活动总结
2014/05/05 职场文书
安全标语口号
2014/06/09 职场文书
2014小学数学教师个人工作总结
2014/12/18 职场文书
长城导游词300字
2015/01/30 职场文书
继续教育心得体会(共6篇)
2016/01/19 职场文书
在容器中使用nginx搭建上传下载服务器
2022/05/11 Servers
Redis特殊数据类型bitmap位图
2022/06/01 Redis