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加密解密示例分享
Jan 29 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十四)
Jun 26 PHP
表单提交错误后返回内容消失问题的解决方法(PHP网站)
Oct 20 PHP
详解WordPress开发中get_header()获取头部函数的用法
Jan 08 PHP
PHP创建文件,并向文件中写入数据,覆盖,追加的实现代码
Mar 25 PHP
PHP编程之设置apache虚拟目录
Jul 08 PHP
centos 7.2下搭建LNMP环境教程
Nov 20 PHP
php_pdo 预处理语句详解
Nov 21 PHP
php获取文件名称和扩展名的方法
Feb 07 PHP
Windows下php+mysql5.7配置教程
May 16 PHP
php微信开发之关注事件
Jun 14 PHP
解决laravel(5.5)访问public报错的问题
Oct 12 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
Yii操作数据库的3种方法
2014/03/11 PHP
如何实现php图片等比例缩放
2015/07/28 PHP
php调用淘宝开放API实现根据卖家昵称获取卖家店铺ID的方法
2015/07/29 PHP
ThinkPHP 框架实现的读取excel导入数据库操作示例
2020/04/14 PHP
javascript中setTimeout的问题解决方法
2014/05/08 Javascript
Javascript基础知识(三)BOM,DOM总结
2014/09/29 Javascript
JQuery中模拟image的ajaxPrefilter与ajaxTransport处理
2015/06/19 Javascript
jQuery与Ajax以及序列化
2016/02/01 Javascript
js获取form表单所有数据的简单方法
2016/08/18 Javascript
JavaScript中关于for循环删除数组元素内容时出现的问题
2016/11/21 Javascript
利用angular.copy取消变量的双向绑定与解析
2016/11/25 Javascript
JS实现加载时锁定HTML页面元素的方法
2017/06/24 Javascript
jQuery实现ajax回调函数带入参数的方法示例
2018/06/26 jQuery
angular 未登录状态拦截路由跳转的方法
2018/10/09 Javascript
微信小程序实现日期格式化和倒计时
2020/11/01 Javascript
微信JS-SDK实现微信会员卡功能(给用户微信卡包里发送会员卡)
2019/07/25 Javascript
原生JS与CSS实现软件卸载对话框功能
2019/12/05 Javascript
使用JS location实现搜索框历史记录功能
2019/12/23 Javascript
浅析微信小程序自定义日历组件及flex布局最后一行对齐问题
2020/10/29 Javascript
Vue中使用JsonView来展示Json树的实例代码
2020/11/16 Javascript
Python爬取当当、京东、亚马逊图书信息代码实例
2017/12/09 Python
Python 读取某个目录下所有的文件实例
2018/06/23 Python
美国在线咖啡、茶和餐厅供应商:LollicupStore
2018/05/04 全球购物
英国创新设计文具、卡片和礼品包装网站:Paperchase
2018/07/14 全球购物
*p++ 自增p 还是p所指向的变量
2016/07/16 面试题
护理专业毕业生自我鉴定
2013/10/08 职场文书
平面设计师的工作职责
2013/11/21 职场文书
打架检讨书800字
2014/01/10 职场文书
竞聘上岗演讲稿范文
2014/01/10 职场文书
俄罗斯商务邀请函
2014/01/26 职场文书
市优秀教师事迹材料
2014/02/05 职场文书
浪漫婚礼主持词
2014/03/14 职场文书
关爱残疾人演讲稿
2014/05/24 职场文书
青年文明号口号
2014/06/17 职场文书
2015年会计工作总结范文
2015/05/26 职场文书
Python中的变量与常量
2021/11/11 Python