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函数
Dec 06 PHP
AJAX for PHP简单表数据查询实例
Jan 02 PHP
PHP 数字左侧自动补0
Mar 31 PHP
解析php多线程下载远程多个文件
Jun 25 PHP
php生成excel文件的简单方法
Feb 08 PHP
PHP5中实现多态的两种方法实例分享
Apr 21 PHP
四种php中webservice实现的简单架构方法及实例
Feb 03 PHP
php解析url并得到url中的参数及获取url参数的四种方式
Oct 26 PHP
PHP中的表达式简述
May 29 PHP
24条货真价实的PHP代码优化技巧
Jul 28 PHP
PHP中SERIALIZE和JSON的序列化与反序列化操作区别分析
Oct 11 PHP
Laravel 5.5 的自定义验证对象/类示例代码详解
Aug 29 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
PHP中构造函数和析构函数解析
2014/10/10 PHP
qTip 基于JQuery的Tooltip插件[兼容性好]
2010/09/01 Javascript
读jQuery之六 缓存数据功能介绍
2011/06/21 Javascript
推荐9款炫酷的基于jquery的页面特效
2014/12/07 Javascript
JS实现的自定义右键菜单实例二则
2015/09/01 Javascript
利用Chrome DevTools直接调试Node.js和JavaScript的方法详解(并行)
2017/02/16 Javascript
jQuery插件Echarts实现的双轴图效果示例【附demo源码下载】
2017/03/04 Javascript
JS仿淘宝搜索框用户输入事件的实现
2017/06/19 Javascript
在小程序中使用canvas的方法示例
2018/09/17 Javascript
深入理解令牌认证机制(token)
2019/08/22 Javascript
[42:25]2018DOTA2亚洲邀请赛 4.5 淘汰赛 LGD vs Liquid 第三场
2018/04/06 DOTA
python list 合并连接字符串的方法
2013/03/09 Python
常用python数据类型转换函数总结
2014/03/11 Python
在Python中操作列表之List.pop()方法的使用
2015/05/21 Python
Python实现简单字典树的方法
2016/04/29 Python
python实现分页效果
2017/10/25 Python
python实现发送邮件功能代码
2017/12/14 Python
PyCharm配置mongo插件的方法
2018/11/30 Python
Django集成CAS单点登录的方法示例
2019/06/10 Python
调试Django时打印SQL语句的日志代码实例
2019/09/12 Python
python 实现多线程下载视频的代码
2019/11/15 Python
python中setuptools的作用是什么
2020/06/19 Python
Python 实现简单的客户端认证
2020/07/29 Python
Python 通过正则表达式快速获取电影的下载地址
2020/08/17 Python
css3 仿写阿里云水纹效果的示例代码
2018/02/10 HTML / CSS
浅谈html5增强的页面元素
2016/06/14 HTML / CSS
英国领先的餐饮折扣俱乐部:Gourmet Society
2020/07/26 全球购物
如何在存储过程中使用Loop
2016/01/05 面试题
SQL数据库笔试题
2016/03/08 面试题
如何为DataGridView添加一个定制的Column Type
2014/01/21 面试题
某/etc/fstab文件中的某行如下: /dev/had5 /mnt/dosdata msdos defaults,usrquota 1 2 请解释其含义
2013/04/11 面试题
幼儿教师个人求职信范文
2013/09/21 职场文书
三年大学自我鉴定
2014/01/16 职场文书
舞蹈教师自荐信
2014/01/27 职场文书
医科大学毕业生自荐信
2014/02/03 职场文书
大专学生求职信
2014/07/04 职场文书