0

我正在创建一个 PHP 事件日历。我的日历工作正常,但我的任何活动都不会显示(现在我在 2013 年 2 月 28 日只有一个测试活动)

我感觉它与 DATE_FORMAT 有关,但我能得到的任何帮助将不胜感激。

您可以在http://nitelifeconcepts.com/scrg/calendar.php查看日历

原始代码来自http://davidwalsh.name/php-event-calendar。阅读评论,看起来其他人也有同样的问题,但他们的解决方案似乎都没有对我有用

我的代码:

<html>
<head>
<title>SCRG EVENTS</title>
<link rel="stylesheet" type="text/css" href="calendar.css">
<script type="text/javascript">
    var GB_ROOT_DIR = "http://nitelifeconcepts.com/scrg/greybox/";
</script>
<script type="text/javascript" src="greybox/AJS.js"></script>
<script type="text/javascript" src="greybox/AJS_fx.js"></script>
<script type="text/javascript" src="greybox/gb_scripts.js"></script>
<link href="greybox/gb_styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
/* Open up a connection to the mysql database on the same server as website */
$db_link = mysql_connect(XXXX, XXX, XXX)
    or die("Unable to connect to mysql database");

/* Select our database (there is more than one in my server) */
mysql_select_db("db156115_scrg", $db_link);
/* draws a calendar */
function draw_calendar($month,$year,$events = array()){

  /* 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">&nbsp;</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"><div style="position:relative;height:100px;">';
      /* add in the day number */
      $calendar.= '<div class="day-number">'.$list_day.'</div>';

    $event_day = $year.'-'.$month.'-'.$list_day;
      if(isset($events[$event_day])) {
        foreach($events[$event_day] as $event) {
          $calendar.= '<div class="event">'.$event['Title'].'</div>';
        }
      }
      else {
        $calendar.= str_repeat('<p>&nbsp;</p>',2);
      }
    $calendar.= '</div></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">&nbsp;</td>';
    endfor;
  endif;

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


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

  /** DEBUG **/
  $calendar = str_replace('</td>','</td>'."\n",$calendar);
  $calendar = str_replace('</tr>','</tr>'."\n",$calendar);

  /* all done, return result */
  return $calendar;
}

function random_number() {
  srand(time());
  return (rand() % 7);
}

/* date settings */
$month = ($_GET['month'] ? $_GET['month'] : date('m'));
$year = ($_GET['year'] ? $_GET['year'] : date('Y'));
if($month < 10)
  $month = '0'.$month;

/* select month control */
$select_month_control = '<select name="month" id="month">';
for($x = 1; $x <= 12; $x++) {
  $select_month_control.= '<option value="'.$x.'"'.($x != $month ? '' : '   selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$year)).'</option>';
}
$select_month_control.= '</select>';

/* select year control */
$year_range = 7;
$select_year_control = '<select name="year" id="year">';
for($x = ($year-floor($year_range/2)); $x <= ($year+floor($year_range/2)); $x++) {
  $select_year_control.= '<option value="'.$x.'"'.($x != $year ? '' : '    selected="selected"').'>'.$x.'</option>';
}
$select_year_control.= '</select>';

/* "next month" control */
$next_month_link = '<a href="?month='.($month != 12 ? $month + 1 : 1).'&year='.($month !=   12 ? $year : $year + 1).'" class="control">Next Month &gt;&gt;</a>';

/* "previous month" control */
$previous_month_link = '<a href="?month='.($month != 1 ? $month - 1 : 12).'&year='.($month   != 1 ? $year : $year - 1).'" class="control">&lt;&lt;   Previous Month</a>';


/* bringing the controls together */
$controls = '<form method="get">'.$select_month_control.$select_year_control.'&nbsp;<input    type="submit" name="submit" value="Go"   />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$previous_month_link.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.      $next_month_link.' </form>';

/* get all events for the given month */
$events = array();
$query = "SELECT Title, DATE_FORMAT(eventDate,'%y-%m-%d') AS eventDate FROM eventcalendar    WHERE eventDate LIKE '$year-$month'";
$result = mysql_query($query,$db_link) or die('cannot get results!');
while($row = mysql_fetch_assoc($result)) {
  $events[$row['eventDate']][] = $row;
}

echo '<h2 style="float:left; padding-  right:30px;">'.date('F',mktime(0,0,0,$month,1,$year)).' '.$year.'</h2>';
echo '<div style="float:left;">'.$controls.'</div>';
echo '<div style="clear:both;"></div>';
echo draw_calendar($month,$year,$events);
?>
</body>
</html>

再次感谢您提供的任何帮助

4

1 回答 1

0

尝试将您的日期设置位更改为:

if(isset($_GET['month'])) {
$month = $_GET['month'];
} else {
$month = date('m');
}
$month = str_pad($month,2,0,STR_PAD_LEFT);

if(isset($_GET['year'])) {
$year = $_GET['year'];
} else {
$year = date('Y');
}

并摆脱这一点...

if($month < 10)
$month = '0'.$month;

我应该警告你,我不是一个编码员!!!

于 2013-02-28T14:08:15.463 回答