简单实用PHP日历程序代码
发布:smiling 来源: PHP粉丝网 添加日期:2015-04-11 17:11:08 浏览: 评论:0
日历程序代码我们一般会使用一些js插件来实现了,但是像博客这种日志分类我们会使用php程序来实现,下面php粉丝网就来为你介绍一下吧.
PHP日历程序,功能都是大众化的,可以下拉切换年月,上一年下一月下一年上一月,太另类的没去写,主要的写出来了,扩展起来就方便多了,标题为什么要叫精美呢,是因自已感觉界面还过得去,哈哈,让大家见笑了,不足之处还请指出.
php日历核心代码如下:
- <?php
- //日历类
- class calendar {
- //当前的年
- private $year;
- //当前的月
- private $month;
- //一个月中第一天是星期几
- private $start_weekday;
- //当前月的天数
- private $days;
- //最大数与最小年数,最大与最小月数
- private $yearMonth = array(2080, 1900, 12, 1);
- //构造函数
- function __construct() {
- if (isset($_GET['year'])) {
- $this->year = $_GET['year'];
- }
- if (isset($_GET['month'])) {
- $this->month = $_GET['month'];
- }
- $this->pnYm($this->year, $this->month);
- $this->days = date('t', mktime(0, 0, 0, $this->month, 1, $this->year));
- $this->start_weekday = date('w', mktime(0, 0, 0, $this->month, 1, $this->year));
- $this->style();
- }
- //输出
- private function style() {
- echo '<table id="calendar">';
- $this->weeklist();
- $this->daylist();
- echo '<table>';
- }
- //年月参数判断
- private function ymCheck($year, $month) {
- if (!is_numeric($year)) {
- $year = date('Y');
- }
- if (!is_numeric($month)) {
- $month = date('m');
- }
- if ($month < $this->yearMonth[3]) {
- $month = $this->yearMonth[2];
- $year -= 1;
- }
- if ($month > $this->yearMonth[2]) {
- $month = $this->yearMonth[3];
- $year = intval($year) + 1;
- }
- $year = $year < $this->yearMonth[1] ? $this->yearMonth[1] : $year;
- $year = $year > $this->yearMonth[0] ? $this->yearMonth[0] : $year;
- return array($year, $month);
- }
- //上一年、下一年、上一月、下一月
- private function pnYm($year, $month) {
- $ym = $this->ymCheck($year, $month);
- $this->year = $ym[0];
- $this->month = $ym[1];
- }
- //weeklist周列表
- private function weeklist() {
- $week = array('日','一','二','三','四','五','六');
- echo '<tr>';
- foreach ($week as $val) {
- echo '<th>'.$val.'</th>';
- }
- echo '</tr>';
- }
- //daylist天列表
- private function daylist() {
- //年月日导航
- echo '<tr>';
- echo '<td><a title="上一年" href="?year='.($this->year-1).'&month='.$this->month.'"><<</a></td>';
- echo '<td><a title="上一月" href="?year='.$this->year.'&month='.($this->month-1).'"><</a></td>';
- echo '<td colspan="3">';
- echo '<form action="?" method="get" id="form">';
- echo '<select name="year" onchange="formaction()">';
- for ($i = $this->yearMonth[1]; $i <= $this->yearMonth[0]; $i++) {
- if ($i == $this->year) {
- echo '<option value="'.$i.'" selected="selected">'.$i.'年</option>';
- }else {
- echo '<option value="'.$i.'">'.$i.'年</option>';
- }
- }
- echo '</select>';
- echo '<select name="month" onchange="formaction()">';
- for ($i = $this->yearMonth[3]; $i <= $this->yearMonth[2]; $i++) {
- if ($i == $this->month) {
- echo '<option value="'.$i.'" selected="selected">'.$i.'月</option>'; //开源软件:phpfensi.com
- }else {
- echo '<option value="'.$i.'">'.$i.'月</option>';
- }
- }
- echo '</select></form></td>';
- echo '<td><a title="下一月" href="?year='.$this->year.'&month='.($this->month+1).'">></a></td>';
- echo '<td><a title="下一年" href="?year='.($this->year+1).'&month='.$this->month.'">>></a></td>';
- echo '</tr>';
- echo '<tr>';
- //输出空格(当前一个月第一天前面要空出来的)
- for($i = 0; $i < $this->start_weekday; $i++) {
- echo '<td> </td>';
- }
- for ($k = 1; $k <= $this->days; $k++) {
- $i++;
- if ($k == date('d')) {
- echo '<td>'.$k.'</td>';
- }else {
- echo '<td>'.$k.'</td>';
- }
- if ($i % 7 == 0) {
- if ($k != $this->days) {
- echo '</tr><tr>';
- }
- }
- }
- echo '</tr>';
- }
- }
- ?>
html+css代码如下:
- <!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=utf-8" />
- <title>PHP日历程序</title>
- <style>
- #calendar { width:auto; margin:0 auto; margin-top:100px; border:0; border-collapse:collapse; box-shadow:0px 0px 4px #ddd; font-size:12px; text-align:center; font-family:"微软雅黑"; color:#333; border:solid 1px #c5e2ff; }
- #calendar tr { width:auto; height:34px; line-height:34px; }
- #calendar tr th { width:44px; background:#c5e2ff; }
- #calendar tr td { background:#fff; }
- #calendar tr td.tdbg { background:#c5e2ff; }
- #calendar tr td:hover { background:#FFC; }
- #calendar tr td a { text-decoration:none; color:#f50; font-weight:900; }
- #calendar select { width:auto; border:solid 1px #c5c5c5; padding:2px 0 2px 0; background:#fff; float:left; margin-left:5px; }
- </style>
- <script>
- function formaction() {
- var form = document.getElementById('form');
- form.submit();
- }
- </script>
- </head>
- <body>
- <?php
- require 'init.php';
- $calendar = new calendar();
- ?>
- </body>
- </html>
Tags: PHP日历程序 PHP日历代码
- 上一篇:php系统时区设置方法总结
- 下一篇:php网页时间比实际慢8个小时解决办法
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)