Jump to content

Custom PHP calendar - Integer to Month String?


PHP Learner

Recommended Posts

I wrote the following code to create a set of 12 php calendars to display specific entries from a database. I have found many posts about converting a date into an integer but can find nothing to convert an integer into a date or more specifically in this case a month.

 

The interesting thing about all of this is that it accepts and integer as a string in a concatinated set of values initially but won't accept the $month after the change at the end of the code when it returns to continue the loop. Can anyone tell me where I'm going wrong here?

<?php
		
		$month = date("m"); // Initate $month to current month
		
		$twelveCount = '1'; // Set count to 1 for loop 
		
		
		while($twelveCount != '13') { // Create loop condition to break at 13 producing 12 calendars
		
		$monthName = date("F", strtotime($month)); // Grab name of month from $month variable
		
		$year = date("Y"); // Initiate $year from current year
		
		$firstDate = $month . '/01/' . $year; // Calculate first day of the month, NOTE: It accepts the string integer here;
		
		$firstDay = date("D", strtotime($firstDate)); // Grab day of the first date

		$firstDay = strtoupper($firstDay); // Capitalise first day string
		
		$lastDay = cal_days_in_month(CAL_GREGORIAN, $month, $year); // Grab last day of the month

		$i = '1'; // Set integer to 1 for begining of auto incremental date display
		
		$lastDayPlusOne = $lastDay + 1; // Set limit for loop break when days are complete
		

		if ($firstDay == 'SUN') {$skipDays = '0';} // Skip spaces on calendar to equal actual first day
		if ($firstDay == 'MON') {$skipDays = '1';}
		if ($firstDay == 'TUE') {$skipDays = '2';}
		if ($firstDay == 'WED') {$skipDays = '3';}
		if ($firstDay == 'THU') {$skipDays = '4';}
		if ($firstDay == 'FRI') {$skipDays = '5';}
		if ($firstDay == 'SAT') {$skipDays = '6';}
		
		$count = '0'; // Iniate $count for loop break when first day is in the correct position
		

		$calendarDisplay .= '<div style="float:left; width:182px; margin:0px 0px 7px 7px;">
		
		<div style="float:left; width:161px; text-align:left; margin-bottom:5px;">' . $monthName . ' ' . $year . '</div>
		<div style="float:left; width:23px; margin:0px 3px 3px 0px; background-color:#CCC; font-size:10px; text-align:center;">SUN</div>
		<div style="float:left; width:23px; margin:0px 3px 3px 0px; background-color:#CCC; font-size:10px; text-align:center;">MON</div>
		<div style="float:left; width:23px; margin:0px 3px 3px 0px; background-color:#CCC; font-size:10px; text-align:center;">TUE</div>
		<div style="float:left; width:23px; margin:0px 3px 3px 0px; background-color:#CCC; font-size:10px; text-align:center;">WED</div>
		<div style="float:left; width:23px; margin:0px 3px 3px 0px; background-color:#CCC; font-size:10px; text-align:center;">THU</div>
		<div style="float:left; width:23px; margin:0px 3px 3px 0px; background-color:#CCC; font-size:10px; text-align:center;">FRI</div>
		<div style="float:left; width:23px; margin:0px 3px 3px 0px; background-color:#CCC; font-size:10px; text-align:center;">SAT</div>
		
		<div style="float:left; width:182px;">'; // Create day names for header
		
		while($count != $skipDays) { // Output spaces to match day of the week before dates begin
			
			$calendarDisplay .= '<div style="float:left; width:23px; padding:4px 0; margin:0px 3px 3px 0px;"></div>';
			
			$count++;
		}
		
		
		
		
		while($i != $lastDayPlusOne) { // Output dates inline with days in header
			
			$calendarDisplay .= '<div style="float:left; padding:4px 0; width:23px; margin:0px 3px 3px 0px; background-color:#0C0; font-size:10px; text-align:center;">' . $i . '</div>';
			
			$i++;
		}
		
		$calendarDisplay .= '</div></div>'; // End calendar display
		
		
		// This is where it all goes wrong... 
		
		if ($month == date("m", strtotime("1/10/10"))) {$month = date("m", strtotime("2/10/10")); echo $month;}
		if ($month == '02') {$month = date("m", strtotime("3/10/10")); echo $month;}
		if ($month == '03') {$month = '04'; echo $month;}
/*		if ($month == '04') {$month = date("m", strtotime("5/10/10")); echo $month;}
		if ($month == '05') {$month = date("m", strtotime("6/10/10")); echo $month;}
		if ($month == '06') {$month = date("m", strtotime("7/10/10")); echo $month;}
		if ($month == '07') {$month = date("m", strtotime("8/10/10")); echo $month;}
		if ($month == '08') {$month = date("m", strtotime("9/10/10")); echo $month;}
		if ($month == '09') {$month = date("m", strtotime("10/10/10")); echo $month;}
		if ($month == '10') {$month = date("m", strtotime("11/10/10")); echo $month;}
		if ($month == '11') {$month = date("m", strtotime("12/10/10")); echo $month;}
		if ($month == '12') {$month = date("m", strtotime("01/10/10")); echo $month;}
*/

		$twelveCount++; // Increment $twelveCount by 1

		
		} // End while($twelveCount != '13') {
		
		
		echo $calendarDisplay; // Echo result out to page

?>

Here's an alternative method

<?php
function makeCalendar($dateStart)
{
    $dateEnd = new DateTime($dateStart->format('Y-m-t'));
    $dateEnd->modify('+1 days');
    $dp = new DatePeriod($dateStart, new DateInterval('P1D'), $dateEnd);
    $calArray = array_fill(0,7,'');
    
    echo '<div class="month">';
    echo "<h4>" . $dateStart->format('F Y') . "<h4>";
    echo "<table border='1' style='border-collapse:collapse'>";
    $days = array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa');
    echo "<tr><th>" . join('</th><th>', $days) . '</th></tr>';
    
    foreach ($dp as $d) {
        $dow = $d->format('w');
        $calArray[$dow] = $d->format('j');
        if ($dow==6) {
            echo '<tr><td>' . join('</td><td>', $calArray).'</td></tr>';
            $calArray = array_fill(0,7,'');
        }
    }
    if ($dow!= 6) echo '<tr><td>' . join('</td><td>', $calArray).'</td></tr>';
    echo "</table></div>\n";
}


?>
<html>
<head>
<style type="text/css">
th,td {
    width: 23px;
    font-family: sans-serif;
    font-size: 9pt;
    text-align: center;
}
h4 {
    font-family: sans-serif;
    font-size: 9pt;
}
div.month {
    float:left; 
    width:24%; 
    height: 180px; 
    margin:0px 0px 7px 7px;
}
</style>
</head>
<body>
    <?php
        $dt1 = new DateTime('first day of this month');
        $dp1 = new DatePeriod($dt1, new DateInterval('P1M'), 11);
        foreach ($dp1 as $month) {
            makeCalendar($month);
        }
    ?>
</body>
</html>

post-3105-0-96029100-1389702306_thumb.png

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.