Jump to content

calendar code


jimfog

Recommended Posts

I am constructing a calendar and i am in the process of making that portion that resembles the left portion of outlook(or hotmail calendar). This is the place where you have a row depicting the month,below the letter initials(M,T,W) and below are td tags for the days.(1,2,3,4)... I hope you are understand what i am talking about. Here is the code:

  $timestamp = mktime(0, 0, 0,date("n", strtotime($arg)), 1, $cYear);//     $thismonth = getdate($timestamp);     $startday = $thismonth['wday'];     $maxday = date("t", $timestamp);echo '<tr >										<td align="center">SU</td>										<td align="center" >MO</td>										<td align="center" >TU</td>										<td align="center" >WE</td>										<td align="center" >TH</td>										<td align="center" >FR</td>										<td align="center" >SA</td>									</tr>'; 		 for ($i = 0; $i < ($maxday + $startday); $i++){		 if (($i % 7) == 0) 			 echo "<tr>\n";		 if ($i < $startday) 			 echo "<td></td>\n";			   else{			 echo "<td class='calendar-day' align='center' valign='middle' height='20px'>" . ($i - $startday + 1) . "</td>\n";		 }   if (($i % 7) == 6)			 echo "</tr>\n";	 } 	 echo '</table>';

The problem above is that i want to fix the days from where the printing starts, now it starts from Sunday, i want them to start from Monday-as is the case with outlook. For example, in the above code, the 4th of March(under the Sunday column) is depicted far left while i want it in far right.So, going the sunday column far right is easy, but what about the days associated with it? Thanks for the patience.

Link to comment
Share on other sites

I am very close at achieving it, here is the revised code:

echo '<tr >																			    <td align="center" >ΔΕ</td>									    <td align="center" >ΤΡ</td>									    <td align="center" >ΤΕ</td>									    <td align="center" >ΠΕ</td>									    <td align="center" >ΠΑ</td>									    <td align="center" >ΣΑ</td>									   <td align="center">ΚΥ</td>								    </tr>';		 for ($i = 0; $i < (($maxday-1) + $startday); $i++) {		 if (($i % 7) == 0)// όταν η μέρες είναι 7 στον αρθμό άνοιξε row			 echo "<tr class='juveline'>\n";		 if ($i < $startday-1)			 echo "<td></td>\n"; //όταν οι μέρες είναι λιγότερες από την 1η μερα μιας εβδομάδα δημιούργησε κενά κουτάκια         else {			 echo "<td class='calendar-day' align='center' valign='middle' height='20px'>" . ($i - $startday + 2) . "</td>\n";		 }		 if (($i % 7) == 6)			 echo "</tr>\n";	 }	 echo '</table>';

The problem remains with the months that start from Sunday,(April for example), 1 is not showed, the calendar prints directly to 2(namely Monday).

Link to comment
Share on other sites

When you're printing the first week you need to figure out which day the first is on. If the first isn't the first day to print then you need to print however many days from the previous month to get to the first day of the current month. If the month starts on Sunday, and Sunday is the last day you display, then for the first week you need to print the 6 last days from the previous month and then the first of the current month.

Link to comment
Share on other sites

When you're printing the first week you need to figure out which day the first is on. If the first isn't the first day to print then you need to print however many days from the previous month to get to the first day of the current month. If the month starts on Sunday, and Sunday is the last day you display, then for the first week you need to print the 6 last days from the previous month and then the first of the current month.
I understand what are you talking about and maybe this is better-but how am i going to do it. So far where the days of the previous month(or next) should depicted i have placed empty <td> cells.
Link to comment
Share on other sites

... I usually do a grey or lighter color with a faded date.
I believe this is the way to go(now that i am thinking it again). Any help(code) is welcome.
Link to comment
Share on other sites

Do a count for every cell, but because you start from sunday you will have to start from 7 $cellcount = 7, then compare the start day (as in date("N", $date); // numeric value of day sat = 6, sunday = 7) value to $cellcount, if they match echo out date, else echo empty cell, now when $cellcount has processed the values for sunday it has to be reset to 1 to represent monday value, loop through this until all dates for specific month are echo out, then if $cellcount !=6 loop through remaining to produce empty cells to finish of table.

Link to comment
Share on other sites

as stated above I do no want anymore empty cells but instead days from the previous/next month.I am trying to achieve that.

Link to comment
Share on other sites

Here's one i prepared earlier, it even adds class names to apply style to previous and next month residue dates and also weekends, it works from monday to sunday, so you will have to redo it to your requirement of starting from sunday.

<?phpget_calendar_months(1,1, 2012);function get_calendar_months($startmonth, $endmonth, $startyear){$currentyear="";$currentmonth="";$currentday="";$dayarray = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");for($year=$startyear;$year<$startyear+1;$year++){	  for($mon=$startmonth;$mon<=$endmonth;$mon++)   {   if($mon != $currentmonth)	{	$month = mktime(0,0,0,$mon, 1, $year);	 if($year != $currentyear)  {  ?>  <table border="0">	 <tr>	   <th scope="col" colspan="7" class="yearheader"><?php echo $year; ?></th>	 </tr> 	   <tr>		<td colspan="7" class="monthheader"><?php echo date("F", $month); ?></td>	  </tr><tr>	  <?php	for($weekday=0;$weekday<7;$weekday++)	 {	 if($weekday==5 || $weekday==6)	 {	 $cellclass="weekend";	 }	else	 {	 $cellclass="weekdays";	 }		 ?>	  		<td class="<?php echo $cellclass; ?>"><?php echo substr($dayarray[$weekday],0,1); ?></td>		   <?php	 }		   ?>	  </tr>	  <?php//////////////////week days	 $startdate = date("N", $month); // numeric value of day sat = 6, sunday = 7	$totaldays_in_mon = date("t", $month);	$prev_totaldays_in_mon = date("t", $month-1);	$prev_mon_residue = ($prev_totaldays_in_mon-$startdate)+2;  	$datecount =0;	$colcout=0;	$rowcount=0;	$next_month_residue=0;	 for($date=1;$date<($totaldays_in_mon+$startdate);$date++)	 {	 $colcout++;		 if($colcout ==1)	  {	  ?>	  <tr>	  <?php	  }	 if($date>=$startdate)	  {	  $datecount++;	  if($colcout==6 || $colcout==7)	   {	   $cellclass="weekend";	   }	  else	   {	   $cellclass="weekdays";	   }	  ?>		 <td class="<?php echo $cellclass; ?>" id="<?php echo 'dateid_'.$year.str_pad(date("m", $month), 2 , "0", STR_PAD_LEFT).str_pad($datecount, 2 , "0", STR_PAD_LEFT); ?>"><?php echo $datecount; ?></td>			<?php	  }	  elseif($colcout==6 || $colcout==7)	  {	  ?>	  <td class="mon_weekend_residue"><?php echo $prev_mon_residue; ?></td>	  <?php	  }	 else	  {		  ?>	  <td class="mon_weekdays_residue"><?php echo $prev_mon_residue; ?></td>	  <?php	  $prev_mon_residue++;	  }		 if($colcout % 7 ==0)	  {	  $colcout=0;	  $rowcount++;	  ?>	  </tr>	  <?php	  }		 }	 if($colcout < 7 && $colcout !=0)	  {		 for($emptycells=$colcout;$emptycells < 7;$emptycells++)	  {	  $next_month_residue++;	  if($emptycells ==5 || $emptycells ==6)	  {	  echo '<td class="mon_weekend_residue">'.$next_month_residue.'</td>';	  }	  else	  {	  echo '<td class="mon_weekdays_residue">'.$next_month_residue.'</td>';	  }		  }	   echo '</tr>';	   $rowcount++;	  }	}  /*  if($rowcount<=6)	{$next_month_residue++;	for($j=$rowcount;$j<6;$j++)	{  	echo '<tr><td class="mon_weekdays_residue">'.$next_month_residue++.'</td><td class="mon_weekdays_residue">'.$next_month_residue++.'</td><td class="mon_weekdays_residue">'.$next_month_residue++.'</td><td class="mon_weekdays_residue">'.$next_month_residue++.'</td><td class="mon_weekdays_residue">'.$next_month_residue++.'</td><td class="mon_weekend_residue">'.$next_month_residue++.'</td><td class="mon_weekend_residue">'.$next_month_residue++.'</td></tr>';	$rowcount++;	}	}*/     $currentmon = $mon;   }	?>        </table>  <?php  }$currentyear = $year;}} ?>

Link to comment
Share on other sites

thanks for the code-my example starts also from Monday,not Sunday.I hope it will be able to fit with the code you had given me some weeks ago,for calculating days,months etc.

Link to comment
Share on other sites

remove

 if($rowcount<=6) 	{$next_month_residue++;	for($j=$rowcount;$j<6;$j++)	{  	echo '<tr><td class="mon_weekdays_residue">'.$next_month_residue++.'</td><td class="mon_weekdays_residue">'.$next_month_residue++.'</td><td class="mon_weekdays_residue">'.$next_month_residue++.'</td><td class="mon_weekdays_residue">'.$next_month_residue++.'</td><td class="mon_weekdays_residue">'.$next_month_residue++.'</td><td class="mon_weekend_residue">'.$next_month_residue++.'</td><td class="mon_weekend_residue">'.$next_month_residue++.'</td></tr>';	$rowcount++;	}	}

I can't remember why its there? but it produce to many reside next month dates if left in.

Link to comment
Share on other sites

remove
if($rowcount<=6)  	{$next_month_residue++;	for($j=$rowcount;$j<6;$j++)	{  	echo '<tr><td class="mon_weekdays_residue">'.$next_month_residue++.'</td><td class="mon_weekdays_residue">'.$next_month_residue++.'</td><td class="mon_weekdays_residue">'.$next_month_residue++.'</td><td class="mon_weekdays_residue">'.$next_month_residue++.'</td><td class="mon_weekdays_residue">'.$next_month_residue++.'</td><td class="mon_weekend_residue">'.$next_month_residue++.'</td><td class="mon_weekend_residue">'.$next_month_residue++.'</td></tr>';	$rowcount++;	}	}

I can't remember why its there? but it produce to many reside next month dates if left in.

Sorry but there is sth i do not understand in your code, calling your function with these parameters:
get_calendar_months(1,1, 2012);

will output january, of course i want always the current month to be displayed, now for example it is March, so i tried this:

get_calendar_months(1,3, 2012);

The above prints all months from January through March, something i do not want of course. Was this your original intention? Unless you wrote the specific function as general guide ONLY. Obviously, i must pass in the function as a parameter the current month,but, probably this means reworking/rewrite the function from the beginning, in some aspects.

Link to comment
Share on other sites

No, i want only the current month displayed. Should i modify it you think or is it better to use entirely different code. Thanks for your help anyway.

Link to comment
Share on other sites

you could use the current code, all you have to do is put the month you require for start and end arguments, that why i usedget_calendar_months(1,1, 2012);to show month of January only, you could use 2,2, 2012 for February and so on, if you want you can strip it down to remove the code that use the end month to produce the other months, up to you.

Link to comment
Share on other sites

Your code is OK, the only thing i do not understand why when the dayarray contains the names of the days in greek the printed outcome is not the desired one.Look the photo to see what i am talking about:https://skydrive.live.com/redir.aspx?cid=be27434b2aac8130&resid=BE27434B2AAC8130!227&parid=BE27434B2AAC8130!140 Where the red arrow points the days of the week were supposed to printed-their initials actually- but now look what happens.

Link to comment
Share on other sites

The weekdays in array return as question marks, its only when change the page charset to ISO-8859-7, and use iconv() did they come out to what i think is right ( had to 0 to 2 characters with substr(), instead of 0-1 for M = Monday, T =Tuesday etc)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-7" /><body><?phpecho iconv("UTF-8","ISO-8859-7", "Sunday =  Κυριακή (KiriakEE)<br />Monday =  Δευτέρα (DeFTEra)<br />Tuesday = Τρίτη (TREEtee)<br />Wednesday = Τετάρτη (TeTArtee)<br />Thursday = Πέμπτη (PEmptee)<br />Friday = Παρασκευή (ParaskeVEE)<br />Saturday = Σάββατο (SAvatoh)");get_calendar_months(1,10, 2012);echo '<br/>Τρίτη<br/>';function get_calendar_months($startmonth, $endmonth, $startyear){$currentyear="";$currentmonth="";$currentday="";$dayarray = array("Δευτέρα", "Τρίτη", "Τετάρτη", "Πέμπτη", "Παρασκευή", "Σάββατο", "Κυριακή");for($year=$startyear;$year<$startyear+1;$year++){	  for($mon=$startmonth;$mon<=$endmonth;$mon++)   {   if($mon != $currentmonth)    {    $month = mktime(0,0,0,$mon, 1, $year);	 if($year != $currentyear)  {  ?>  <table border="0">	 <tr>	   <th scope="col" colspan="7" class="yearheader"><?php echo $year; ?></th>	 </tr> 	   <tr>	    <td colspan="7" class="monthheader"><?php echo date("F", $month); ?></td>	  </tr><tr>	  <?php    for($weekday=0;$weekday<7;$weekday++)	 {	 if($weekday==5 || $weekday==6)	 {	 $cellclass="weekend";	 }    else	 {	 $cellclass="weekdays";	 }		 ?>	  	    <td class="<?php echo $cellclass; ?>"><?php echo iconv("UTF-8","ISO-8859-7" ,substr($dayarray[$weekday],0,2)); ?></td>	 	   <?php	 }		   ?>	  </tr>	  <?php//////////////////week days	 $startdate = date("N", $month); // numeric value of day sat = 6, sunday = 7    $totaldays_in_mon = date("t", $month);    $prev_totaldays_in_mon = date("t", $month-1);    $prev_mon_residue = ($prev_totaldays_in_mon-$startdate)+2;       $datecount =0;    $colcout=0;    $rowcount=0;    $next_month_residue=0;	 for($date=1;$date<($totaldays_in_mon+$startdate);$date++)	 {	 $colcout++;		 if($colcout ==1)	  {	  ?>	  <tr>	  <?php	  }	 if($date>=$startdate)	  {	  $datecount++;	  if($colcout==6 || $colcout==7)	   {	   $cellclass="weekend";	   }	  else	   {	   $cellclass="weekdays";	   }	  ?>		 <td class="<?php echo $cellclass; ?>" id="<?php echo 'dateid_'.$year.str_pad(date("m", $month), 2 , "0", STR_PAD_LEFT).str_pad($datecount, 2 , "0", STR_PAD_LEFT); ?>"><?php echo $datecount; ?></td>	 	    <?php	  }	  elseif($colcout==6 || $colcout==7)	  {	  ?>	  <td class="mon_weekend_residue"><?php echo $prev_mon_residue; ?></td>	  <?php	  }	 else	  {	 	  ?>	  <td class="mon_weekdays_residue"><?php echo $prev_mon_residue; ?></td>	  <?php	  $prev_mon_residue++;	  }	 	 if($colcout % 7 ==0)	  {	  $colcout=0;	  $rowcount++;	  ?>	  </tr>	  <?php	  }	 	 }	 if($colcout < 7 && $colcout !=0)	  {	 	 for($emptycells=$colcout;$emptycells < 7;$emptycells++)	  {	  $next_month_residue++;	  if($emptycells ==5 || $emptycells ==6)	  {	  echo '<td class="mon_weekend_residue">'.$next_month_residue.'</td>';	  }	  else	  {	  echo '<td class="mon_weekdays_residue">'.$next_month_residue.'</td>';	  }	 	  }	   echo '</tr>';	   $rowcount++;	  }    }      $currentmon = $mon;   }    ?>        </table>  <?php  }$currentyear = $year;}}?> </body></html>

Link to comment
Share on other sites

OR

<td class="<?php echo $cellclass; ?>"><?php echo mb_substr($dayarray[$weekday],0,1,'UTF-8'); ?></td>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7" /><body><?phpget_calendar_months(1,10, 2012);echo '<br/>Τρίτη<br/>';function get_calendar_months($startmonth, $endmonth, $startyear){$currentyear="";$currentmonth="";$currentday="";$dayarray = array("Δευτέρα", "Τρίτη", "Τετάρτη", "Πέμπτη", "Παρασκευή", "Σάββατο", "Κυριακή");for($year=$startyear;$year<$startyear+1;$year++){	  for($mon=$startmonth;$mon<=$endmonth;$mon++)   {   if($mon != $currentmonth)    {    $month = mktime(0,0,0,$mon, 1, $year); 	 if($year != $currentyear)  {  ?>  <table border="0">	 <tr>	   <th scope="col" colspan="7" class="yearheader"><?php echo $year; ?></th>	 </tr> 	   <tr>	    <td colspan="7" class="monthheader"><?php echo date("F", $month); ?></td>	  </tr><tr>	  <?php    for($weekday=0;$weekday<7;$weekday++)	 {	 if($weekday==5 || $weekday==6)	 {	 $cellclass="weekend";	 }    else	 {	 $cellclass="weekdays";	 }		 ?>	  	   		 <td class="<?php echo $cellclass; ?>"><?php echo mb_substr($dayarray[$weekday],0,1,'UTF-8'); ?></td>		 	   <?php	 }		   ?>	  </tr>	  <?php//////////////////week days	 $startdate = date("N", $month); // numeric value of day sat = 6, sunday = 7    $totaldays_in_mon = date("t", $month);    $prev_totaldays_in_mon = date("t", $month-1);    $prev_mon_residue = ($prev_totaldays_in_mon-$startdate)+2;       $datecount =0;    $colcout=0;    $rowcount=0;    $next_month_residue=0;	 for($date=1;$date<($totaldays_in_mon+$startdate);$date++)	 {	 $colcout++;		 if($colcout ==1)	  {	  ?>	  <tr>	  <?php	  }	 if($date>=$startdate)	  {	  $datecount++;	  if($colcout==6 || $colcout==7)	   {	   $cellclass="weekend";	   }	  else	   {	   $cellclass="weekdays";	   }	  ?>		 <td class="<?php echo $cellclass; ?>" id="<?php echo 'dateid_'.$year.str_pad(date("m", $month), 2 , "0", STR_PAD_LEFT).str_pad($datecount, 2 , "0", STR_PAD_LEFT); ?>"><?php echo $datecount; ?></td>	 	    <?php	  }	  elseif($colcout==6 || $colcout==7)	  {	  ?>	  <td class="mon_weekend_residue"><?php echo $prev_mon_residue; ?></td>	  <?php	  }	 else	  {	 	  ?>	  <td class="mon_weekdays_residue"><?php echo $prev_mon_residue; ?></td>	  <?php	  $prev_mon_residue++;	  }	 	 if($colcout % 7 ==0)	  {	  $colcout=0;	  $rowcount++;	  ?>	  </tr>	  <?php	  }	 	 }	 if($colcout < 7 && $colcout !=0)	  {	 	 for($emptycells=$colcout;$emptycells < 7;$emptycells++)	  {	  $next_month_residue++;	  if($emptycells ==5 || $emptycells ==6)	  {	  echo '<td class="mon_weekend_residue">'.$next_month_residue.'</td>';	  }	  else	  {	  echo '<td class="mon_weekdays_residue">'.$next_month_residue.'</td>';	  }	 	  }	   echo '</tr>';	   $rowcount++;	  }    }      $currentmon = $mon;   }    ?>        </table>  <?php  }$currentyear = $year;}}?> </body></html>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...