doucai5315 2014-07-22 13:52
浏览 49
已采纳

日历功能中的Bughunt

I wrote a Calendar Script that worked pretty well until i wanted to add navigation for last/next month/year. When i called that function with printmonth($m,$y); it worked and showed current month, when i called printmonth($m+1,$y); it showed next month. Now that i added navigation to NOT call function manually it doesn't work, it shows december of current year. Always. It even tries to navigate to 13th month althogh i added if statements to check if it is the last month of the year or first month of the year. Please comment if you see something suspicious, i know its terrible structure and variable names are not pretty, but i'm doing my first steps in PHP.

Thanks for your time and feedback, here is my function:

function printmonth($month,$year) {

// abort if invalid values

if($month>=13) { die("No month >=13 please");   };
if(($year<=1970) || ($year>=3000)) {die("No year <=1970 or >=3000 please");};

// an array of names

$mnames = array(
    1 => "Januar",
    2 => "Februar",
    3 => "März",
    4 => "April",
    5 => "Mai",
    6 => "Juni",
    7 => "Juli",
    8 => "August",
    9 => "September",
    10 => "Oktober",
    11 => "November",
    12 => "Dezember"
);    

// more vars

$month     = intval($month);
$year      = intval($year);    
$thisday   = intval(date('j'));
$thismonth = intval(date('m'));
$thisyear  = intval(date('Y'));

$weekday        = date("N", mktime(0,0,0,$month,"01",$year));
$lastdayofmonth = date('t', mktime(0,0,0,$month,2,$year));
$oneless        = $weekday-1;

/* calculate end of last month */

$lastdayoflastmonth = date('t', mktime(0,0,0,$month-1,2,$year));
$pp=$lastdayoflastmonth-$oneless+1;

$seven = 0+$weekday;

// next year/month... problem must be somewhere in this section

$prevyear = $year-1;
$nextyear = $year+1;
if ($month=1)  { $prevmonth=12;         $nextmonth=$month+1; };
if ($month=12) { $prevmonth=$month-1;   $nextmonth=1;  };
if (($month>=2) || ($month<=11)) {$prevmonth=$month-1; $nextmonth=$month+1;};

// print table head and navigation and day names

echo('<table class="cal">'."
");

echo('<tr class="headnav">'."
");
echo('    <td class="headtd"><a href="?y='.$prevyear.'&m='.$month.'">|<</a></td>'."
");
echo('    <td class="headtd"><a href="?y='.$year.'&m='.$prevmonth.'">&lt;</a></td>'."
");
echo('    <td class="headtd" colspan="3">'.$mnames[$month].' '.$year.'</td>'."
");
echo('    <td class="headtd"><a href="?y='.$year.'&m='.$nextmonth.'">&gt;</a></td>'."
");
echo('    <td class="headtd"><a href="?y='.$nextyear.'&m='.$month.'">>|</a></td>'."
");
echo('</tr>'."
");

echo('<tr class="head">'."
");
echo('   <td class="headtd">Mo</td>'."
");
echo('   <td class="headtd">Di</td>'."
");
echo('   <td class="headtd">Mi</td>'."
");
echo('   <td class="headtd">Do</td>'."
");
echo('   <td class="headtd">Fr</td>'."
");
echo('   <td class="headtd">Sa</td>'."
");
echo('   <td class="headtd">So</td>'."
");
echo('</tr>'."
");

echo(' <tr>'."
");

//print end of last month

for ($l=1;$l<=$oneless;$l++) { 
    echo('<td class="placebegin">'.$pp.'</td>'."
");
    $pp++;
};

// print each day until last day of month

for ($x=1;$x<=$lastdayofmonth;$x++) 
    { 
        if (($x==$thisday) && ($thismonth==$month) && ($thisyear==$year))
            {$bold = 1;} 
                else {$bold = 0;};

        if ($seven<=7) { 
            if ($bold = 1) 
                {      echo('<td class="today"><a href="tage.php?d='.$x.'&m='.$month.'&y='.$year.'" class="tag">'.$x.'</a></td>'."
"); } 
                else { echo('<td class="row">  <a href="tage.php?d='.$x.'&m='.$month.'&y='.$year.'" class="tag">'.$x.'</a></td>'."
");};

            $seven++; 

      } else { 
          echo('</tr><tr>'); 
          $seven = 1; 
          $x--; 
      };

    };

// print numbers until row is complete    

$p = 1;
for ($e=$seven;$e<=7;$e++) { echo('<td class="placebegin">'.$p.'</td>'); $p++; };


// complete table
echo ('   </tr> ');
echo (' </table>');

//done

}; 

//function ends here, the call and some vars:


if (isset($_GET['y']) && (!empty($_GET['y']))) 
{
$y=intval($_GET['y']);
} 
    else 
        {
        $y = intval(date('Y'));
        };

if (isset($_GET['m']) && (!empty($_GET['m']))) 
{
$m=intval($_GET['m']);
} 
    else 
        {
        $m = intval(date('n'));
        };

// call function

printmonth($m, $y);
  • 写回答

1条回答 默认 最新

  • douwojiao5919 2014-07-22 14:49
    关注

    Shouldn't you use

    if($month == 1)//condition
    

    instead of

    if($month = 1)//assignment
    

    as well as the second if.

    The third if must use an && instead of ||, thus replace

    if (($month>=2) || ($month<=11))
    

    by

    if (($month>=2) && ($month<=11))
    

    An assignment succeeds and (if I recall correctly if the variable you assign is set after the operation). Thus $month = 1 succeeds. As as a result, the $prevmonth is set to 12, thus december...

    When I run your function myself, it works... Although one gets a lot of warnings...

    Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead on line 29

    Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead on line 30

    Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead on line 31

    Warning: mktime() [function.mktime]: It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead on line 33

    Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead on line 33

    Warning: mktime() [function.mktime]: It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead on line 34

    Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead on line 34

    Warning: mktime() [function.mktime]: It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead on line 39

    Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead on line 39

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!