PHP简单创建日历的方法

 更新时间:2016年05月03日 10:31:18   作者:懒人  
这篇文章主要介绍了PHP简单创建日历的方法,

本文实例讲述了PHP简单创建日历的方法。分享给大家供大家参考,具体如下:

<?php
function build_calendar($month,$year) {
  // Create array containing abbreviations of days of week.
  $daysOfWeek = array('S','M','T','W','T','F','S');
  // What is the first day of the month in question?
  $firstDayOfMonth = mktime(0,0,0,$month,1,$year);
  // How many days does this month contain?
  $numberDays = date('t',$firstDayOfMonth);
  // Retrieve some information about the first day of the
  // month in question.
  $dateComponents = getdate($firstDayOfMonth);
  // What is the name of the month in question?
  $monthName = $dateComponents['month'];
  // What is the index value (0-6) of the first day of the
  // month in question.
  $dayOfWeek = $dateComponents['wday'];
  // Create the table tag opener and day headers
  $calendar = "<table class='calendar'>";
  $calendar .= "<caption>$monthName $year</caption>";
  $calendar .= "<tr>";
  // Create the calendar headers
  foreach($daysOfWeek as $day) {
     $calendar .= "<th class='header'>$day</th>";
  }
  // Create the rest of the calendar
  // Initiate the day counter, starting with the 1st.
  $currentDay = 1;
  $calendar .= "</tr><tr>";
  // The variable $dayOfWeek is used to
  // ensure that the calendar
  // display consists of exactly 7 columns.
  if ($dayOfWeek > 0) {
     $calendar .= "<td colspan='$dayOfWeek'>&nbsp;</td>";
  }
  $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  while ($currentDay <= $numberDays) {
     // Seventh column (Saturday) reached. Start a new row.
     if ($dayOfWeek == 7) {
       $dayOfWeek = 0;
       $calendar .= "</tr><tr>";
     }
     $currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);
     $date = "$year-$month-$currentDayRel";
     $calendar .= "<td class='day' rel='$date'>$currentDay</td>";
     // Increment counters
     $currentDay++;
     $dayOfWeek++;
  }
  // Complete the row of the last week in month, if necessary
  if ($dayOfWeek != 7) {
     $remainingDays = 7 - $dayOfWeek;
     $calendar .= "<td colspan='$remainingDays'>&nbsp;</td>";
  }
  $calendar .= "</tr>";
  $calendar .= "</table>";
  return $calendar;
}
//调用方法
echo build_calendar(05,2016);
?>

运行结果如下图所示:

关于在线显示日期还可参考本站在线工具:

在线万年历日历

网页万年历日历

在线万年历黄历flash版

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php日期与时间用法总结》、《PHP数学运算技巧总结》、《PHP数组(Array)操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

  • 解决Laravel5.x的php artisan migrate数据库迁移创建操作报错SQLSTATE[42000]

    解决Laravel5.x的php artisan migrate数据库迁移创建操作报错SQLSTATE[42000]

    这篇文章主要介绍了解决Laravel5.x的php artisan migrate数据库迁移创建操作报错SQLSTATE[42000],需要的朋友可以参考下
    2020-04-04
  • ThinkPHP路由详解

    ThinkPHP路由详解

    ThinkPHP路由通俗的说,他是一个url的传输规则,例如:index.php?g=Home&m=Index&a=price 这个规则也是可以变化的,& 可以设置为@或者其他,规则主要有pathinfo等,在设置伪静态的时候也用的着他
    2015-07-07
  • php实现SAE上使用storage上传与下载文件的方法

    php实现SAE上使用storage上传与下载文件的方法

    这篇文章主要介绍了php实现SAE上使用storage上传与下载文件的方法,实例分析了基于SaeStorage类实现文件传输的技巧,需要的朋友可以参考下
    2015-06-06
  • WIN8.1下搭建PHP5.6环境

    WIN8.1下搭建PHP5.6环境

    很多人喜欢用linux搭建php网页语言运行环境,但由于linux高度自定义化,经常需要root运行命令,略显高端,相对应的微软的windows操作系统,用户体验不错,可以借助windows自带的IIS组件+PHP程序包,搭建一个合适的运行环境。
    2015-04-04
  • PHP实现文件上传与下载的示例代码

    PHP实现文件上传与下载的示例代码

    这篇文章主要为大家详细介绍了PHP实现文件上传与下载功能的相关知识,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2023-12-12
  • 详细分析PHP 命名空间(namespace)

    详细分析PHP 命名空间(namespace)

    这篇文章主要介绍了PHP 命名空间(namespace)的的相关资料,文中讲解非常详细,实例代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06
  • PHP面向接口编程 耦合设计模式 简单范例

    PHP面向接口编程 耦合设计模式 简单范例

    了解些面向对象的知识,自己写了段代码测试一下,欢迎高手指点
    2011-03-03
  • PHP CURL实现模拟登陆并上传文件操作示例

    PHP CURL实现模拟登陆并上传文件操作示例

    这篇文章主要介绍了PHP CURL实现模拟登陆并上传文件操作,结合实例形式分析了PHP使用curl进行模拟登陆与文件传输操作具体实现技巧,需要的朋友可以参考下
    2020-01-01
  • 使用PHP的日期与时间函数技巧

    使用PHP的日期与时间函数技巧

    任何一种语言,日期和时间函数都是我们常使用的,下面就教你PHP下的日期和时间的技巧
    2008-04-04
  • PHP 设计模式之观察者模式介绍

    PHP 设计模式之观察者模式介绍

    观察者模式定义对象的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新!
    2012-02-02

最新评论