使用按钮更改日历月

时间:2014-04-03 20:16:33

标签: php html

我正在尝试用PHP制作日历。我有它显示当前月份,但我想添加提交按钮以转到下个月或前几个月。这是我目前的代码:

<!DOCTYPE html>
<html>
<body>
<style>
  /* calendar */
  table.calendar        { border-left:1px solid #999; }
  tr.calendar-row   {  }
  td.calendar-day   { min-height:80px; font-size:11px; position:relative; } * html div.calendar-day { height:80px; }
  td.calendar-day:hover { background:#eceff5; }
  td.calendar-day-np    { background:#eee; min-height:80px; } * html div.calendar-day-np { height:80px; }
  td.calendar-day-head { background:#ccc; font-weight:bold; text-align:center; width:120px; padding:5px; border-bottom:1px solid #999; border-top:1px solid #999; border-right:1px solid #999; }
  div.day-number        { background:#999; padding:5px; color:#fff; font-weight:bold; float:right; margin:-5px -5px 0 0; width:20px; text-align:center; }
  /* shared */
  td.calendar-day, td.calendar-day-np { width:120px; padding:5px; border-bottom:1px solid #999; border-right:1px solid #999; }
</style>
<?php
  error_reporting(E_ERROR | E_PARSE);
  /* draws a calendar */
  function draw_calendar($month,$year){
/* draw table */
$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';

/* table headings */
$headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

/* days and weeks vars now ... */
$running_day = date('w',mktime(0,0,0,$month,1,$year));
$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
$days_in_this_week = 1;
$day_counter = 0;
$dates_array = array();

/* row for week one */
$calendar.= '<tr class="calendar-row">';

/* print "blank" days until the first of the current week */
for($x = 0; $x < $running_day; $x++):
    $calendar.= '<td class="calendar-day-np"> </td>';
    $days_in_this_week++;
endfor;

/* keep going with days.... */
for($list_day = 1; $list_day <= $days_in_month; $list_day++):
    $calendar.= '<td class="calendar-day">';
        /* add in the day number */
        $calendar.= '<div class="day-number">'.$list_day.'</div>';

        /** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !!  IF MATCHES FOUND, PRINT THEM !! **/
        $calendar.= str_repeat('<p> </p>',2);

    $calendar.= '</td>';
    if($running_day == 6):
        $calendar.= '</tr>';
        if(($day_counter+1) != $days_in_month):
            $calendar.= '<tr class="calendar-row">';
        endif;
        $running_day = -1;
        $days_in_this_week = 0;
    endif;
    $days_in_this_week++; $running_day++; $day_counter++;
endfor;

/* finish the rest of the days in the week */
if($days_in_this_week < 8):
    for($x = 1; $x <= (8 - $days_in_this_week); $x++):
            $calendar.= '<td class="calendar-day-np"> </td>';
        endfor;
    endif;

    /* final row */
    $calendar.= '</tr>';

    /* end the table */
    $calendar.= '</table>';

    /* all done, return result */
    return $calendar;
}
$currentmonth = date('m');
//echo "$currentmonth - ";
$currentyear = date('Y');
//echo "$currentyear";
$months = array('January','Feburary','March','April','May','June','July','August','September','October','November','December');
$years = array('2010','2011','2012','2013','2014','2015','2016','2017','2018','2019','2020');
echo "<h2>${months[$currentmonth-1]} ${years[4]} </h2>";
echo draw_calendar($currentmonth,$currentyear);
/* sample usages */
/*
echo '<h2>March 2014</h2>';
echo draw_calendar(3,2014);

echo '<h2>April 2014</h2>';
echo draw_calendar(4,2014);
*/
?>
</body>
</html>

基于一些搜索,看起来这样做的方法是使用会话,就像我下面的内容,但是我不确定这是如何工作的,或者我将如何实现它。感谢您提供的任何帮助。

<?php
  session_start();
  if($_POST['submit']){
     $_SESSION['i'] = isset($_SESSION['i']) ? ++$_SESSION['i'] : 0;
    echo $_SESSION['i'];
  }
?>

1 个答案:

答案 0 :(得分:2)

这是一种将当前月份和年份都存储为SESSION变量的方法,可以按月和年顺利递增和递减。例如。如果你到了2014年12月,下一个按钮将带你到2015年1月。

此代码在您的函数draw_calendar结束后立即开始。这应该让你开始。请注意,我并没有真正使用您的月份和年份数组。我确实使用它一次用于日历上方的标题。但我没有使用岁月数组。如果您愿意,可以稍后加入。我还添加了一个Today按钮,将值重置为当前月份和年份。

session_start(); //don't forget this...
$currentmonth = date('m');
$currentyear = date('Y');

$months = array('January','February','March','April','May','June','July','August','September','October','November','December');
$years = array('2010','2011','2012','2013','2014','2015','2016','2017','2018','2019','2020');

//sets initial value of SESSION variables to current month and year
if(!isset($_SESSION['month'])){
    $_SESSION['month'] = $currentmonth;
}
if(!isset($_SESSION['year'])){
    $_SESSION['year'] = $currentyear;
}

//increments or decrements year based on user input
if(isset($_POST['LastMonth'])){
    if($_SESSION['month']-1 == 0){ 
        --$_SESSION['year'];
    }
    $_SESSION['month'] = ($_SESSION['month']-1 > 0) ? --$_SESSION['month'] : 12;

}

if(isset($_POST['NextMonth'])){
    if ($_SESSION['month']+1 == 13){
        ++$_SESSION['year'];
    }
    $_SESSION['month'] = ($_SESSION['month']+1 < 13) ? ++$_SESSION['month'] : 1;
}

if(isset($_POST['Today'])){
    $_SESSION['year'] = $currentyear;
    $_SESSION['month'] = $currentmonth;
}

//print calendar based on SESSION variables
echo '<h2>'. $months[$_SESSION['month']-1] .' '. $_SESSION['year'].'</h2>';
echo draw_calendar($_SESSION['month'],$_SESSION['year']);


//buttons to move around
echo '<form method="post">
    <input type="submit" name="LastMonth" value="<< Last Month" />
    <input type="submit" name="NextMonth" value="Next Month >>" />
    <input type="submit" name="Today" value="Today" />
    </form>
';

//print_r($_SESSION); //uncomment to keep tabs on your variables