使用PHP生成HTML5日历

Creating a calendar for the archive feature of the next redesign of this blog called for a combination of , , JavaScript and . Previously I’ve discussed the markup and CSS for the calendar; in this entry, I will show the PHP. A server-side language is used as the archive feature will be required for the new blog; Javascript will be added for extra, optional functionality.

为本博客的下一次重新设计创建存档功能的日历,该日历要求结合使用JavaScript之前,我已经讨论了日历的标记和CSS; 在此条目中,我将显示PHP。 新博客需要使用服务器端语言作为存档功能; 将添加Javascript以实现额外的可选功能。

Creating the calendar for an arbitrary month is the perfect role for a function. The renderCalendar function requires just two variables: a year and month. The function will need to determine what day the 1st of the chosen month falls on, and the number of days in the month, filling in the days before and after, if applicable.

创建一个任意一个月的日历是一个完美的角色功能renderCalendar函数仅需要两个变量:年份和月份。 该功能将需要确定何日选择每月的1 适逢以及天在一个月(如果适用)的数量,之前和之后的日子里填写。

Here’s the complete code of the function, heavily adapted from work by Martin Breault.

这是该功能的完整代码,在很大程度上与Martin Breault的工作相适应

<?php 
function renderCalendar($strMonth, $strYear) {
	$date = mktime(12, 0, 0, $strMonth, 1, $strYear);
	$daysInMonth = date("t", $date);
	$dayOffset = date("w", $date);
	$nextday = 1;
	$next = date("Y-m-",strtotime($strYear."-".$strMonth."-01 +1 months"));
	if ($dayOffset > 0) {
		$prevmonthdaystart = date("t",strtotime($strYear."-".$strMonth."-01 -1 months")) - ($dayOffset -1);
		$prev = date("Y-m-",strtotime($strYear."-".$strMonth."-01 -1 months"));
	}
	$calendarWeeks = ceil(($daysInMonth + $dayOffset) / 7) - 1; 
	echo '<h1>'.date("F", $date).' '.$strYear.'</h1>';
	for ($rowIndex = 0; $rowIndex <= $calendarWeeks; $rowIndex++) {
		for ($colIndex = 1; $colIndex <= 7; $colIndex++) {
		$currentDay = ($colIndex - $dayOffset) + ($rowIndex * 7);
		if ($currentDay > $daysInMonth) {
			$after = str_pad($nextday, 2 , "0",STR_PAD_LEFT)
			echo '<time datetime="'.$next.$after.'" class="not month">’;
			echo "<a href="#">'.$after.'</a></time>';
			$nextday++;
		} elseif ($currentDay < 1) { 
			echo '<time datetime="';
			echo $prev.str_pad($prevmonthdaystart, 2 , "0",STR_PAD_LEFT); 
			echo "' class="notmonth"><a href="#">'$prevmonthdaystart.'</a>';
			echo "</time>';
			$prevmonthdaystart++;
			} else {
			$current = str_pad($currentDay, 2 , "0",STR_PAD_LEFT);
			echo '<time datetime="'.$strYear.'-'.str_pad($strMonth, 2 , "0",STR_PAD_LEFT);
			echo '-'.$current.'"><a href="#" id='.$currentDay.'" >';
echo $current.'</a></time>';
			}
		}
	}
}
?>

mktime converts a given date to standard Unix time. (As we don’t need information on a specific day, we use the 1st of the month as an arbitrary starting point). Given that, we can determine the number of days in the chosen month, and when the 1st of the month falls (w as a keyword in the date() function provides an offset for the 1st day in the month: if the 1st falls on a Sunday, the result will be 0, Monday will be 1, Saturday 6, etc.)

mktime将给定日期转换为标准Unix时间。 (由于我们不需要特定日期的信息,因此我们将月份的1 作为任意起点)。 鉴于此,我们可以判断在选上月的天数,并在每月的1 下降(W作为关键字date()函数提供了1天在一个月的偏移:如果1 st属于星期日,结果为0,星期一为1,星期六为6,依此类推)

$nextday starts the count for the days after the current month; $next generates the Unix time for the following month.

$nextday开始计算当月之后的日子; $next生成下个月的Unix时间。

If the chosen month does not start on a Sunday, $prevmonthdaystart determines the appropriate date from the previous month that does fall on a Sunday.

如果所选的月份不是从星期日开始的,则$prevmonthdaystart确定确实在星期日的上个月的适当日期。

$calendarWeeks is a bit of holdover from the original, table-based layout of the calendar, but still useful: it determines how many weeks there are in the chosen month. A series of for loops generates the appropriate <time> tags with date time attributes, links, and visible dates inside each. (str_pad ensures that leading zeros are added where appropriate).

$calendarWeeks$calendarWeeks的原始,基于表的布局的保留部分,但仍很有用:它确定所选月份中有多少周。 一系列的for循环会生成适当的<time>标记,并在其中包含date time属性,链接和可见日期。 ( str_pad确保在适当的位置添加前导零)。

That’s it! Link destinations are left blank for you to complete.

而已! 链接目标留为空白以供您完成。

翻译自: https://thenewcode.com/357/Generating-An-HTML5-Calendar-With-PHP

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值