Hi all,

I have some calendar code I am using and it has the calendar starting on Monday and I need it to start on Sunday.

It $date is in a 2010-11-04 format and when I look at the code, it seems to be getting the month and year, but not the day. I tried to change the start day for the week to 0 instead of 1 and that did not resolve it. Here is the code that I think is setting the weeks.

pclass Calendar {
	public function renderCalendar($date,$events) {
		$firstDay = getdate(mktime(0,0,0,$date['mon'],0,$date['year']));
//		$firstDay = getdate(mktime(0,0,0,$date['mon'],1,$date['year']));
		$lastDay = getdate(mktime(0,0,0,$date['mon']+1,0,$date['year']));
		$displayCalendar = "<table height=600 width=600 border=1>";
		$displayCalendar .= "<tr><th height=7% colspan=7>" . $date['month'] . " - " . $date['year'] . "</th></tr>";
		$displayCalendar .= "<tr class=days align=center>";

	$displayCalendar .= "<td height=7% width=14%>Mon</td><td width=14%>Tue</td><td width=14%>Wed</td><td width=14%>Thur</td><td width=14%>Fri</td><td width=14%>Sat</td><td width=14%>Sun</td></tr>";

	$displayCalendar .= "<tr align=center valign=top>";
	if($firstDay['wday'] == 0)
	$firstDay['wday'] = 7;
	for($i=1;$i<$firstDay['wday'];$i++){
		$displayCalendar .= "<td>&nbsp;</td>";
	}
	$actday = 0;
	for($i=$firstDay['wday'];$i<=7;$i++){
		$actday++;
		$displayCalendar .= "<td height=14%>" . $actday;
		if($date['mon'] < 10) {
			$m = "0" . $date['mon'];
		} else {
			$m = $date['mon'];
		}
		if($actday < 10) {
			$a = "0" . $actday;
		} else {
			$a = $actday;
		}
		$t = $m . "-" . $a . "-" . $date['year'];
		if(isset($events)) {
			foreach($events as $event) {
				if($event['event_date'] == $t)
				$displayCalendar .= "<br /><a href=calendarDemo.php?view=list&id=" . $event['id'] . ">" . $event['event_title'] . "</a>";
			}
		}
		echo "&nbsp;";
		$displayCalendar .= "</td>";
	}
	$displayCalendar .= "</tr>";
	$fullWeeks = floor(($lastDay['mday']-$actday)/7);
	for ($i=0;$i<$fullWeeks;$i++){
		$displayCalendar .= "<tr align=center valign=top height=50>";
		for ($j=0;$j<7;$j++){
			$actday++;
			$displayCalendar .= "<td height=14%>" . $actday;
			if($date['mon'] < 10) {
				$m = "0" . $date['mon'];
			} else {
				$m = $date['mon'];
			}
			if($actday < 10) {
				$a = "0" . $actday;
			} else {
				$a = $actday;
			}
			$t = $m . "-" . $a . "-" . $date['year'];
			if(isset($events)) {
				foreach($events as $event) {
					if($event['event_date'] == $t) {
						$displayCalendar .= "<br /><a href=calendarDemo.php?view=list&id=" . $event['id'] . ">" . $event['event_title'] . "</a>";
					}
				}
			}
			echo "&nbsp;";
			$displayCalendar .= "</td>";
		}
		$displayCalendar .= "</tr>";
	}
	if ($actday < $lastDay['mday']){

		$displayCalendar .= "<tr align=center valign=top>";
		for ($i=0; $i<7;$i++){
			$actday++;
			if($actday <=$lastDay['mday']) {
				$displayCalendar .= "<td height=14%>" . $actday;
				if($date['mon'] < 10) {
					$m = "0" . $date['mon'];
				} else {
					$m = $date['mon'];
				}
				if($actday < 10) {
					$a = "0" . $actday;
				} else {
					$a = $actday;
				}
				$t = $m . "-" . $a . "-" . $date['year'];
				if(isset($events)) {
					foreach($events as $event) {
						if($event['event_date'] == $t)
						$displayCalendar .= "<br /><a href=calendarDemo.php?view=list&id=" . $event['id'] . ">" . $event['event_title'] . "</a>";
					}
				}
				echo "&nbsp;";
				$displayCalendar .= "</td>";
			} else {
				$displayCalendar .= "<td height=14%>&nbsp;</td>";
			}

		}
	} else {
		//$displayCalendar .= "<td height=14%>&nbsp;</td>";
	}
	$displayCalendar .= "</tr></table>";
	return $displayCalendar;
}
}

You can see that I commented out the original line "$firstDay = getdate(mktime(0,0,0,$date['mon'],1,$date['year']));" and changed it to 0, but that did not change anything.

I appreciate the help.

Don

    It looks as if the array ($date) is being passed from somewhere else?

      Here is where the date and events are coming from:

      And were the function is being called. Does this give you the missing pieces?

      if(!isset($_GET['date'])) {
      	$date = getdate();
      } else {
      	$date = explode('-',$_GET['date']);
      	$date = getdate(mktime(0,0,0,$date[0],1,$date[1]));
      }
      
      $nextMonth = getdate($date[0]+2678400);
      $lastMonth = getdate($date[0]-2160000);
      
      
      $events = NULL;
      $query = $db->query("SELECT id,event_title,DATE_FORMAT(event_date,'%m-%d-%Y') as event_date FROM events WHERE event_date >= '" .$date['year'] . "-" . $date['mon'] . "-1'  AND event_date < '" .$nextMonth['year'] . "-" . $nextMonth['mon'] . "-1'");
      while($event = $db->fetch_array($query)) {
      	$events[] = $event;
      }
      
      $calendar = new Calendar;
      echo "<center>";
      echo $calendar->renderCalendar($date,$events);
      echo "</center>";
      
        pclass Calendar {
        	public function renderCalendar($date,$events) {
        		$firstDay = getdate(mktime(0,0,0,$date['mon'],0,$date['year']));
        //		$firstDay = getdate(mktime(0,0,0,$date['mon'],1,$date['year']));
        		$lastDay = getdate(mktime(0,0,0,$date['mon']+1,0,$date['year']));
        		$displayCalendar = "<table height=600 width=600 border=1>";
        		$displayCalendar .= "<tr><th height=7% colspan=7>" . $date['month'] . " - " . $date['year'] . "</th></tr>";
        		$displayCalendar .= "<tr class=days align=center>";
        
        // The calendar headers here should be reset to start with Sunday:
        
        	$displayCalendar .= "<td height=7% width=14%>Mon</td><td width=14%>Tue</td><td width=14%>Wed</td><td width=14%>Thur</td><td width=14%>Fri</td><td width=14%>Sat</td><td width=14%>Sun</td></tr>";
        
        

        Still munching on the rest of it.

          I haven't got this on a server to test (I generally write test scripts and run via CLI), but here's what I'm thinking now (as a test --- no warranty, implied or otherwise 😉 )

          Comment out this block:

          /* 
          if($firstDay['wday'] == 0)
             $firstDay['wday'] = 7;
             for($i=1;$i<$firstDay['wday'];$i++){
             $displayCalendar .= "<td>&nbsp;</td>";
          }
          */

          And try changing the 7 to a 6 in this line:

          for($i=$firstDay['wday'];$i<=7;$i++){
          

          HTH,

            I made the changes you suggested but it did not change anything. The name of the days changed, but that was a text change.

            I did not see any difference in this from the original code.

            pclass Calendar {
            public function renderCalendar($date,$events) {
            $firstDay = getdate(mktime(0,0,0,$date['mon'],0,$date['year']));
            // $firstDay = getdate(mktime(0,0,0,$date['mon'],1,$date['year']));
            $lastDay = getdate(mktime(0,0,0,$date['mon']+1,0,$date['year']));
            $displayCalendar = "<table height=600 width=600 border=1>";
            $displayCalendar .= "<tr><th height=7% colspan=7>" . $date['month'] . " - " . $date['year'] . "</th></tr>";
            $displayCalendar .= "<tr class=days align=center>";

            // The calendar headers here should be reset to start with Sunday:

                $displayCalendar .= "<td height=7% width=14%>Mon</td><td width=14%>Tue</td><td width=14%>Wed</td><td width=14%>Thur</td><td width=14%>Fri</td><td width=14%>Sat</td><td width=14%>Sun</td></tr>";[/QUOTE]

            I commented out and change the lines below that you suggested. Am I missing something?

            Thanks,
            Don

              Well, Don, I'm not at all sure.

              I took your code from the very first post, removed the class container and ran it as a script. I changed the Headers section so it was Sun first, and compared it with the output of "cal(1)".

              I didn't find any difference ... so if it's still not working there, I'm not sure what's up.

                I think I am still not understanding your code. I set it up on our server as you wrote it. cal1 and it shows the correct day for the 1st (monday), but has 31 days this month?

                And what format is this looking for with the $date var?
                '2010-11-05'
                1288967829

                I like how you ran this on a CLI. I never used one, but it seems a lot faster than me testing locally on my server.

                Thanks,
                Don

                  lol ... I'm not understanding it either, as I didn't write it. It my littlest one's bday today, so I may not get back to this very quickly.

                    Np, I understand. And tell your little one, happy birthday.

                    Its my wifes birthday today as well.

                    Don

                      Well the large integer computes to "Fri, 05 Nov 2010 09:37:09" in my local TZ (US CDT). The 2010-11-05 is a MySQL (or some version of SQL) timestamp, and the large integer is a UNIX timestamp (seconds since the "Epoch", Jan 1, 1970). Probably this was the current time when you (or I) last ran the script.

                      I'm still working on a commented version. Maybe sometime.

                        Write a Reply...