Jump to content

depicting days of the month


jimfog
 Share

Recommended Posts

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...