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中使用Sockets 从Usenet中获取文件
Jan 10 PHP
PHP 压缩文件夹的类代码
Nov 05 PHP
PHP IN_ARRAY 函数使用注意事项
Jul 24 PHP
MySQL时间字段究竟使用INT还是DateTime的说明
Feb 27 PHP
三个类概括PHP的五种设计模式
Sep 05 PHP
php使用curl发送json格式数据实例
Dec 17 PHP
PHP获取一个字符串中间一部分字符的方法
Aug 19 PHP
使用PHP生成二维码的方法汇总
Jul 22 PHP
php基于自定义函数记录log日志方法
Jul 21 PHP
php解压缩zip和rar压缩包文件的方法
Jul 10 PHP
php求斐波那契数的两种实现方式【递归与递推】
Sep 09 PHP
Thinkphp 框架扩展之驱动扩展实例分析
Apr 27 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中利用XML技术构造远程服务(上)
2006/10/09 PHP
php mail to 配置详解
2014/01/16 PHP
php实现使用正则将文本中的网址转换成链接标签
2014/12/03 PHP
仿猪八戒网左下角的文字滚动效果
2011/10/28 Javascript
php+js实现倒计时功能
2014/06/02 Javascript
javascript格式化指定日期对象的方法
2015/04/21 Javascript
jQuery实现表单步骤流程导航代码分享
2015/08/28 Javascript
JS闭包与延迟求值用法示例
2016/12/22 Javascript
深入理解AngularJS中的ng-bind-html指令
2017/03/27 Javascript
js调用刷新界面的几种方式
2017/05/03 Javascript
AngularJS+bootstrap实现动态选择商品功能示例
2017/05/17 Javascript
详解Angular5 服务端渲染实战
2018/01/04 Javascript
node实现基于token的身份验证
2018/04/09 Javascript
JavaScript callback回调函数用法实例分析
2018/05/08 Javascript
vue实现压缩图片预览并上传功能(promise封装)
2019/01/10 Javascript
json数据格式常见操作示例
2019/06/13 Javascript
react 不用插件实现数字滚动的效果示例
2020/04/14 Javascript
微信小程序实现电子签名并导出图片
2020/05/27 Javascript
[01:00:52]2018DOTA2亚洲邀请赛 4.4 淘汰赛 EG vs LGD 第一场
2018/04/05 DOTA
python中定义结构体的方法
2013/03/04 Python
使用Python求解最大公约数的实现方法
2015/08/20 Python
Python虚拟环境项目实例
2017/11/20 Python
python实现log日志的示例代码
2018/04/28 Python
python ddt数据驱动最简实例代码
2019/02/22 Python
浅谈python标准库--functools.partial
2019/03/13 Python
实例讲解Python中整数的最大值输出
2019/03/17 Python
Python with标签使用方法解析
2020/01/17 Python
使用Python实现牛顿法求极值
2020/02/10 Python
奥地利婴儿用品和玩具购物网站:baby-markt.at
2020/01/26 全球购物
日本最大的购物网站乐天市场国际版:Rakuten Global Market(支持中文)
2020/02/03 全球购物
会计职业生涯规划范文
2014/01/04 职场文书
经典婚礼主持开场白
2014/03/13 职场文书
交通安全寄语大全
2014/04/08 职场文书
小学雷锋月活动总结
2014/07/03 职场文书
Vue全家桶入门基础教程
2021/05/14 Vue.js
Mysql基础知识点汇总
2021/05/26 MySQL