Reputation: 11
I am a beginner learning PHP and I am trying to make a basic calendar with PHP. Simpele kalender is how it has to look. It has to be with if and/or loops. This is what I have so far:
<?php
$month_start = 'Tue';
$number_days = 28;
// number of rows
$number_rows = $number_days / 7;
if ($number_days % 7 != 0) {
// number of days
$number_rows = ($number_days / 7) + 1;
}
?>
<table>
<tr>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
<th>Sat</th>
<th>Sun</th>
</tr>
<?php
for($y = 1; $y <= $number_rows;$y++){
echo "<tr>";
for($i = 1; $i <= $number_days; $i++){
if($i % 7 != 0){
echo "<td>". $i . "</td>";
}
else{
echo "<td>". $i . "</td>";
break;
}
}
echo "</tr>";
}
?>
</table>
<style>
table, th, td {
border: 1px solid black;
text-align: center;
}
</style>
How do I start on Thursday (beginning of the month) and how can I get the days 1 to 28 in the columns? I made another calendar but that wasn't correct. According to the feedback I got on it, I don't need to use gmdate, cal_days_in_month and mktime. Anyone that can help me go into the right direction?
Upvotes: 1
Views: 1234
Reputation: 145
This is how I created a calendar. This creates "non-days" i.e. greyed-out (you'll have to style yourself) squares so that the first day of the month will start on its proper weekday place.
$year = 2022;
$date = new DateTime("".$year."-01-01");
$weekday = (int)$date->format('N');
$weekn = (int)$date->format('W');
$day = 1;
$month = 1;
$dayLabels = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
// This creates a "table head" for each month
$thead = '<tr><th>M</th><th>W</th>';
foreach($dayLabels as $lbl) $thead .= "<th>".$lbl."</th>";
$thead .= '</tr>';
// This variable will hold the html table of the calendar
$calendar = '<table id="calendar" border="1" style="width: 550px; max-width: 100%;">';
// Loop each month until 12
for(;$month <= 12; $month++)
{
$calendar .= $thead;
$monthday = 1;
$monthmax = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$position = 0;
while($monthday <= $monthmax)
{
// Create month and week columns
if($position == 0)
{
$weekn = (int)$date->format('W');
$calendar .= "<tr><th>".$month."</th><th>".$weekn."</th>";
$position++;
} // Create "non-days"
else if($position < $weekday)
{
$calendar .= "<td class=\"nonday\"></td>";
$position++;
} // Create days
else if($position == $weekday)
{
$calendar .= "<td class=\"day\">".$monthday."</td>";
$position++;
$weekday++;
$monthday++;
$date->modify('+1 day');
if($position == 8)
{
$position = 0;
$weekday = 1;
}
}
}
// Finish "non-days"
while($position != 0)
{
$calendar .= "<td class=\"nonday\"></td>";
$position++;
if($position == 8) $position = 0;
}
$calendar .= "</tr>";
if($month != 12) $calendar .= '<td colspan="9" class="monthlinebreak"></td>';
}
return $calendar;
Upvotes: 0
Reputation: 125
you can use if($loopCount++ > number) to start DAY inside LOOP
This is an example
$count = 1;
foreach($colum as $res)
{
echo'<td>';
if($count++ > 3){echo $days++;} //will start in the third loop
echo'</td>';
}
Apply Like This
<?php
//TOTAL DAY ON THIS MONTH
$total_days = 30;
//WHERE DAY START
$start_days = 3;
//TOTAL COLUM OF CALENDAR
$col = 27; //27 because this will be an array, because the array has 0 so the total will be 28
for($x = 0; $x <= $col; $x++)
{
$colum[] = $x;
}
echo "<table><tr>";
$days = 1;
$count = 1;
foreach($colum as $res)
{
echo'<td>';if($count++ > $start_days){echo $days++;} echo'</td>';
if ($count++ % 7 == 0)
{
echo "</tr><tr>";
}
}
//NEX MONTH START WITH THIS
$rest_of_the_day = $total_days - $days + 1;
echo"</tr></table>";
Upvotes: 0
Reputation: 13533
One option is to give the days of the week numbers: Mon=0, Tues=1, etc. You can set a variable $start_day = 3;
where 3 is for Thursday. Then add that to get the total number of rows: $number_rows = ceil(($start_day + $number_days) / 7);
Lastly, create a variable to mark the current day number to be printed. Initialize it to -$start_day
and only print if not negative:
$number_days = 28;
$start_day = 3; // Mon=0, Tues=1, etc
// Add in $start_day her to account for the "negative" days before the 1st day
$number_rows = ceil(($start_day + $number_days) / 7);
$current_day = -$start_day;
for ($row = 0; $row < $number_rows; $row++) {
echo("<tr>");
for ($col = 0; $col < 7; $col++) {
echo("<td>");
// Only print the day number if it is not a "negative" day and is not past the end
if ($current_day++ >= 0 && $current_day <= $number_days) {
echo($current_day);
}
echo("</td>");
}
echo("</tr>");
}
Upvotes: 2