genetess aquino genetess aquino - 28 days ago 18
PHP Question

PHP Calendar table

Hi Im trying to make a calendar table that displays the months in a year and days in a week.

I am able to make one for this month(Nov 2016). Now I want to loop it through out the year and for coming years.

Can someone help me?

<?php
/* Set the default timezone */
date_default_timezone_set("Asia/Hong_Kong");

/* Set the date */
$date = strtotime(date("Y-m-d"));

$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);

// $nextyear = strtotime('+1 month', $date);

$firstDay = mktime(0,0,0,$month, 1, $year);
$title = strftime('%B', $firstDay);
$dayOfWeek = date('D', $firstDay);
$daysInMonth = cal_days_in_month(0, $month, $year);
/* Get the name of the week days */
$timestamp = strtotime('next Sunday');
$weekDays = array();



for ($i = 0; $i < 32; $i++) {
$weekDays[] = strftime('%a', $timestamp);
$timestamp = strtotime('+1 day', $timestamp);
}
$blank = date('w', strtotime("{$year}-{$month}-01"));
?>




<table class='table table-bordered' style="table-layout: fixed;">
<tr>
<th colspan="32" class="text-center"> <?php echo $title ?> <?php echo $year ?> </th>
</tr>
<tr>
<?php foreach($weekDays as $key => $weekDay) : ?>
<td class="text-center"><?php echo $weekDay ?></td>
<?php endforeach ?>
</tr>
<tr>
<?php for($i = 0; $i < $blank; $i++): ?>
<td></td>
<?php endfor; ?>
<?php for($i = 1; $i <= $daysInMonth; $i++): ?>
<?php if($day == $i): ?>
<td><strong><?php echo $i ?></strong></td>
<?php else: ?>
<td><?php echo $i ?></td>
<?php endif; ?>
<?php if(($i + $blank) % 32 == 0): ?>
</tr><tr>
<?php endif; ?>
<?php endfor; ?>
<?php for($i = 0; ($i + $blank + $daysInMonth) % 32 != 0; $i++): ?>
<td></td>
<?php endfor; ?>
</tr>
</table>

Answer

As said in the comments above you can use two loops nested inside each other, one for the years, one for the months. That way you can execute your already given code based on different dates but the current one.

Have a look at this approach. It basically does what you are looking for, but you need to do some fine tuning for the layout:

<?php

define('NUMBER_OF_COLUMNS', 37);    

function renderCalenderMonth($date) {
  $day = date('d', $date);
  $month = date('m', $date);
  $year = date('Y', $date);

  $firstDay = mktime(0,0,0,$month, 1, $year);
  $title = strftime('%B', $firstDay);
  $dayOfWeek = date('D', $firstDay);
  $daysInMonth = cal_days_in_month(0, $month, $year);
  /* Get the name of the week days */
  $timestamp = strtotime('next Sunday');
  $weekDays = array();

  for ($i = 0; $i < NUMBER_OF_COLUMNS; $i++) {
      $weekDays[] = strftime('%a', $timestamp);
      $timestamp = strtotime('+1 day', $timestamp);
  }
  $blank = date('w', strtotime("{$year}-{$month}-01"));
  ?>

  <table class='table table-bordered' style="table-layout: fixed;">
      <tr>
      <th colspan="<?php echo NUMBER_OF_COLUMNS?>" class="text-center"> <?php echo $title ?> <?php echo $year ?> </th>
      </tr>
      <tr>
      <?php foreach($weekDays as $key => $weekDay) : ?>
          <td class="text-center"><?php echo $weekDay ?></td>
      <?php endforeach ?>
      </tr>
      <tr>
      <?php for($i = 0; $i < $blank; $i++): ?>
          <td></td>
      <?php endfor; ?>
      <?php for($i = 1; $i <= $daysInMonth; $i++): ?>
          <?php if($day == $i): ?>
          <td><strong><?php echo $i ?></strong></td>
          <?php else: ?>
          <td><?php echo $i ?></td>
          <?php endif; ?>
          <?php if(($i + $blank) % NUMBER_OF_COLUMNS == 0): ?>
          </tr><tr>
          <?php endif; ?>
      <?php endfor; ?>
      <?php for($i = 0; ($i + $blank + $daysInMonth) % NUMBER_OF_COLUMNS != 0; $i++): ?>
          <td></td>
      <?php endfor; ?>
      </tr>
  </table>

  <?php
}

// ===========================

/* Set the default timezone */
date_default_timezone_set("Asia/Hong_Kong");

for ($iterateYear=2016; $iterateYear<2018; $iterateYear++) {
  for ($iterateMonth=1; $iterateMonth<=12; $iterateMonth++) {

    /* Set the date */
    $date = strtotime(sprintf('%s-%s-01', $iterateYear, $iterateMonth));
    renderCalenderMonth($date);

  }
}