Click to See Complete Forum and Search --> : First Calendar script


bigdaddysheikh
08-03-2007, 12:23 AM
Hey,

This is my first ever proper script. After 4 years of copying and pasting I am finally trying to learn how to do thing correctly. I read over 3 tutorials on how to make a calendar. The logic I did not understand due to it being complex. I am uncertain why they used arrays or classes but then again I do not know what they are.

I am just starting my path on this coding properly and getting my logic down. So any help or areas I need to improve let me know guys.

<?

//day of the month
$date = date("d");
$month = date("m");
$year = date("Y");

//getting the first day
$daysInMonth = date('t', mktime(0,0,0,$month,1,$year));
//numerical value of the first day
$firstDay = date("w", mktime(0,0,0,$month,1,$year));
//number of weeks in the month
$rows = ceil($daysInMonth/7);


$htmlValue = "<div id='calendar'>
<span><h2>August</h2></span>
<div id='cal_layout'>
<table>
<thead>
<tr>
<th>S</th>
<th>M</th>
<th>T</th>
<th>W</th>
<th>T</th>
<th>F</th>
<th>S</th>
</tr>
</thead>
<tbody>";
for ($i=1; $i<=$rows; $i++){
$htmlValue .= "<tr>";
for ($j=0; $j<=6; $j++){
//counting for the number of days
$counter++;

$days = $counter;

$days -= $firstDay;

if($days >= 1 && $days <= $daysInMonth) {
$htmlValue .= "<td><a href='' title='Sunday'>$days</a></td>";
}else{
$htmlValue .= "<td></td>";
}
}
$htmlValue .= "</tr>";
}
"</tbody></table></div></div>";
?>

sonicfusion
08-17-2007, 10:09 AM
Personally...

I would have named your $date variable $day.

mktime does not need to be repeated.
[assuming you renamed $date to $day]
$date = mktime(0,0,0,$month,1,$year)
$daysInMonth = date('t', $date);
$firstDay = date('w', $date);

In the html you have August in plain text... this should perhaps be date('F', $date) or date('F', mktime(0,0,0,$month,1,$year))?

An excellent start though! Looks like it will work great.

Weedpacket
08-18-2007, 10:16 AM
I am finally trying to learn how to do thing correctly.

I am uncertain why they used arrays or classes but then again I do not know what they are.

So any help or areas I need to improve let me know guys.

I think you yourself have already identified a major area where you need to improve!