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 = "
$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);
?>
運行結果如下圖所示:
關于在線顯示日期還可參考本站在線工具:
希望本文所述對大家PHP程序設計有所幫助。
總結
以上是生活随笔為你收集整理的php如何生成一年的日历表_PHP简单创建日历的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 烤仔TVのCCW丨密码学通识(三)选择明
 - 下一篇: XCP实战系列介绍14-基于Vector