php怎么添加日历,PHP简单创建日历的方法

news/2025/2/23 19:45:51

本文实例讲述了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 = "

$calendar .= "

$monthName $year";

$calendar .= "

";

// Create the calendar headers

foreach($daysOfWeek as $day) {

$calendar .= "

$day";

}

// Create the rest of the calendar

// Initiate the day counter, starting with the 1st.

$currentDay = 1;

$calendar .= "

";

// The variable $dayOfWeek is used to

// ensure that the calendar

// display consists of exactly 7 columns.

if ($dayOfWeek > 0) {

$calendar .= "

 ";

}

$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 .= "

";

}

$currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);

$date = "$year-$month-$currentDayRel";

$calendar .= "

$currentDay";

// Increment counters

$currentDay++;

$dayOfWeek++;

}

// Complete the row of the last week in month, if necessary

if ($dayOfWeek != 7) {

$remainingDays = 7 - $dayOfWeek;

$calendar .= "

 ";

}

$calendar .= "

";

$calendar .= "

";

return $calendar;

}

//调用方法

echo build_calendar(05,2016);

?>

运行结果如下图所示:

eee5bae97403025fc19a086ac10f3a0f.png

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

在线万年历日历

网页万年历日历

在线万年历黄历flash版

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

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


http://www.niftyadmin.cn/n/711678.html

相关文章

SpringBoot——SpringBoot中设置字符集编码的两种方式

1.方式一&#xff08;使用传统的Spring提供的字符集过滤器&#xff09; 先写一个Servlet。 package com.songzihao.springboot.servlet;import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import j…

win8.1弹框

在winform或者wp8中用MessageBox.Show()弹框,但是wp8.1中已经不存在了 private async void btn_Click(object sender, RoutedEventArgs e) { //首先在需要点击的button按钮Click事件中创建MessageDialog对象 MessageDialog msnew MessageDialog("点击按…

Java设计模式(八)----代理模式

代理模式 1.生活中&#xff1a; 代理就是一个人或者一个组织代表其它人去做一件事的现实生活中的。在一些情况下&#xff0c;一个客户不想或者不能够直接引用一个对象&#xff0c;而代理对象能够在client和目标对象之间起到中介的作用。2.官方&#xff1a; 代理模式是对象的…

Android练习——Spinner二级联动_城市选择

样式部分xml 1 <RelativeLayout xmlns:android"http://schemas.android.com/apk/res/android"2 xmlns:tools"http://schemas.android.com/tools"3 android:layout_width"match_parent"4 android:layout_height"match_parent…

01_MUI之Boilerplate中:HTML5演示样例,动态组件,自己定义字体演示样例,自己定义字体演示样例,图标字体演示样例...

&#xfeff;&#xfeff;1安装HBuilder5.0.0,安装后的界面截图例如以下&#xff1a;2 依照https://www.muicss.com/docs/v1/css-js/boilerplate-html中的说明&#xff0c;创建上图的Boilerplate.html&#xff1a;3 代码内容例如以下&#xff1a;<!--作者&#xff1a;XXXqq.…

matlab功能块的作用,为什么功能块、功能要被拖入组织块才有效?

回答者&#xff1a;henry.wang - 高级工程师&nbsp&nbsp第11级 2018-12-21 21:40:09需要调用才能起作用啊回答者&#xff1a;紫方 - 资深顾问&nbsp&nbsp第13级 2018-12-21 22:58:06从表象上看似乎不一定&#xff0c;功能块也可以调用功能块及功能&#xff0c;但…

第九周项目6-穷举法之百钱百鸡

鸡翁一值钱五&#xff0c;鸡母 一值钱三&#xff0c;鸡雏三值钱一。百钱买百鸡&#xff0c;问鸡翁、鸡母、鸡雏各几何? 构建代码&#xff1a; /**Copyright (c) 2014,烟台大学计算机学院*All gight reserved.*文件名称&#xff1a;temp.cpp*作者&#xff1a;邵帅*完成时间&…

关于Mysql DATE_FORMAT() 日期格式

定义和用法 DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据。 语法 DATE_FORMAT(date,format) date 参数是合法的日期。format 规定日期/时间的输出格式。 格式描述%a缩写星期名%b缩写月名%c月&#xff0c;数值%D带有英文前缀的月中的天%d月的天&#xff0c;数值(00-31)…