jimfog Posted July 6, 2012 Share Posted July 6, 2012 I have concluded in a code that prints that days of a month(format('j')). Actually I want to make the calendar have a month view as Ms-Outlook has.The only issue is that I am trying to find an efficient way to to make the row change every 7 days. here is the code: <table id="month"> <?php for($fstdmonth;$fstdmonth<=$lstdmonth;$fstdmonth->modify('+1 day')->format('d')) { echo '<div class="monthday" >'.$fstdmonth->format('j').'</div>'; if($fstdmonth->format('j')==6 )//this is just for testing {echo '<br>';} } ?></table> Tell me what you think about the above-do you think is a good way for printing the days of the month ANDwhat do you suggest for creating a separate row for every days? Link to comment Share on other sites More sharing options...
boen_robot Posted July 6, 2012 Share Posted July 6, 2012 There is DatePeriod exactly for this purpose. You should adjust the start of the month to start off the week properly though. So something like: $now = new DateTime($newdate);//the code below depicts last and first day of month$fstdmonth=clone $now;$lstdmonth=clone $now;$fstdmonth->modify('first day of this month');$lstdmonth->modify('last day of this month');if (1 < $fstdmonth->format('w')) { $fstdmonth->modify('Monday this week');} elseif (0 == $fstdmonth->format('w')) { $fstdmonth->modify('Monday previous week');}echo '<div>', $fstdmonth->format('r'), '</div><div>',$lstdmonth->format('r'),'</div>';//Just for testing purposes?><table id="month"><thead><tr><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th><th>S</th></tr></thead><tbody><?php$daily = new DateInterval('P1D');foreach (new DatePeriod($fstdmonth, new DateInterval('P1W'), $lstdmonth) as $weekStart) { echo '<tr>'; foreach (new DatePeriod($weekStart, $daily, 6) as $weekday) { echo '<td'; if ($weekday->format('n') !== $lstdmonth->format('n')) { echo ' class="extraDay"'; } echo '>', $weekday->format('j'), '</td>'; } echo '</tr>';}?></tbody></table> where the "extraDay" class is added to let you style dates from other months differently. With a small adjustment, you could instead create empty cells, but IMHO, having the numbers there is somewhat helpful for the user experience, as long as you use the class to style the other months' days differently. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.