Tag Archives: php

Very simple PHP calendar

Many of the calendar scripts out there are un-necessarily complicated & weighty.

If you want a tiny calendar script that is easy to understand and can be adapted to a variety of purposes, here’s one we wrote for our Doozy CMS that might be a good starting point for you.

The PHP:

Usage: echo calendar(10, 2014); // month, year[, timezone] (enter the timezone using standard PHP timezone strings)

function calendar($m, $y, $timezone = 'Pacific/Auckland') { // set your default timezone here
  $date = new DateTime('now', new DateTimeZone($timezone));
  $today = array('d' => $date->format('d'), 'm' => $date->format('m'), 'y' => $date->format('Y'));
  $date->setDate($y, $m, 1);
    $calendar = '
    <table class="calendar"><tr class="month-heading"><th><a class="prev" href="?m='.$date->modify('-1 month')->format('n').'&amp;y='.$date->format('Y').'">&#9664;</a></th><th colspan="5" class="big">' . $date->modify('+1 month')->format('F Y') . '</th><th><a class="next" href="?m='.$date->modify('+1 month')->format('n').'&amp;y='.$date->format('Y').'">&#9654;</a></th></tr>
    <tr class="week-heading"><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr><tr>
    ';
    $month = array('this_month_start_dow' => $date->modify('-1 month')->format('w'), 'this_month_days' => $date->format('t'), 'prev_month_days' => $date->modify('-1 month')->format('t'));
  $days_arr = array();
  $day_ctr = -1;

  // previous month
  if ($month['this_month_start_dow'] > 0) {
    for ($i = $month['this_month_start_dow'] - 1; $i >= 0; $i--) {
      $day_ctr += 1;
      $class = ($day_ctr % 7 == 0 || $day_ctr % 7 == 6) ? 'weekend-day other-month' : 'other-month'; // Sat/Sun
      $days_arr[] = '<td class="' . $class . '">' . ($month['prev_month_days'] - $i) . '</td>';
    }
  }

  // current month
  for ($i = 1; $i <= $month['this_month_days']; $i++) {
    $day_ctr += 1;
    $class = ($day_ctr % 7 == 0 || $day_ctr % 7 == 6) ? 'weekend-day' : ''; // Sat/Sun
    $class .= ($i == $today['d'] && $m == $today['m'] && $y == $today['y']) ? ' today' : ''; // today
    $days_arr[] = '<td class="' . $class . '">' . $i . '</td>';
  }

  // next month
  if (count($days_arr) % 7 != 0) {
    for ($i = 1; $i <= count($days_arr) % 7; $i++) {
      $day_ctr += 1;
      $class = ($day_ctr % 7 == 0 || $day_ctr % 7 == 6) ? 'weekend-day other-month' : 'other-month'; // Sat/Sun
      $days_arr[] = '<td class="' . $class . '">' . $i . '</td>';
    }
  }

  // create the grid
  foreach ($days_arr as $k => $day) {
    $calendar .= (($k) % 7 == 0 ? '</tr><tr>' : '') . $day;
  }

  $calendar .= '</tr></table>';
  return $calendar;
}

Add a bit of CSS

<style>
  .calendar {width:220px;background:#fdfdfd;font:14px arial; color:#644; border:1px solid #dcdcdc; border-collapse:collapse;}
  .month-heading {text-align:center; height:28px; line-height:28px; background:#b53934; color: #fff;}
  .big {font-size:120%;}
  .week-heading {font-size:11px; height:20px; line-height:20px; background:#e6e7ec; text-align:center;}
  td {width:14.2857%;text-align:right;padding:4px!important; border:1px solid #dcdcdc;}
  .other-month {color: #dcdcdc;}
  .weekend-day {background:#fafafa;}
  .today {background:#a6daf0;}
</style>

And there you have it – an easy to use, flexible and customisable calendar. Using the above css should result in something that looks like this:

calendar

Many other options can be easily added, for example in our Doozy CMS events module we include clickable links on days that have events along with an optional summary of events for the selected month below the calendar.

Hope it’s of use to you…

Very quick & dirty way to password protect PHP page content

I definitely don’t recommend using this technique for hiding anything at all sensitive, but if you have a situation where you want certain content to be available or presented in a certain way to people who have a password but not the general public, this one line of code will do the trick:

<?php
$hidden_stuff = 'This could be text, data obtained from a database or whatever else you like or simply used to format the page in a certain way...';

echo (strpos($_SERVER['QUERY_STRING'], 'sneakypassword') !== false ? '<p>' . $hidden_stuff . '</p>' : '<p>Stuff that anybody can see.</p>');

People who are allowed to view the hidden content don’t need to log in – all they need to do is add the password to the URL as a querystring.

eg: http://yoursite.com/pagewithhiddenstuff.php?sneakypassword

There are plenty of good reasons not to do this:

  • The password is visible in the URL so can be seen by others nearby
  • It is more susceptable to brute force password attacks than a posted password
  • The password will be bookmarked if the page is
  • The password will be cached or logged if the page is

On the other hand, it can have it’s uses. I personally use this exact technique for dummy lorem ipsum text generation for website testing. I have a page that generates random words in a format suitable for pasting straight into a visual text editor, if I add a ?p to the url, it wraps the paragraphs in <p> elements, if I add ?ul or ?ol it outputs it as an unordered or ordered list.

I may post the code for this in a future post.