31.3. PHP Examples

This section provides four examples of how PHP can be used with XHTML to provide dynamic content. Using the examples in this section, you should be able to construct a solid foundation of PHP skills to interact with other technologies and create rich content for delivery over the Web.

This section uses the same sample data outlined in Chapter 28, "Using CGI." See the "Sample Data" section in that chapter for more details on the sample data used for the examples here.

31.3.1. Date and Time Handling

This example shows how the PHP date and time routines can be used to create a simple XHTML calendar. This example displays the current month, with the current day in red.

For a more dynamic calendar example, see the third example in this section.

Example 1: A Simple Calendar

Source

<?php

// Set up the variables (today, month, year, cmd)
//  today = m/d/y
function setvars() {

  // Default is today
  $today = getdate();
  $month = $today['mon'];
  $year = $today['year'];
  $today = $month."/".$today['mday']."/".$year;

  // Return vars in an array
  return array("today" => $today,
               "month" => $month,
               "year" => $year);

}  // End setvars()


// Do the document header
function docheader($month,$year) {

  $month_text =  date("F",strtotime($month."/1/".$year));

  // If content header has not been sent,
  //   send it
  if (!headers_sent()) {
    header('Content-type: text/html');
  }

// Print the document header (up to first date row)
print <<<HTML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml1.dtd"> ...

Get Web Standards Programmer's Reference: HTML, CSS, JavaScript®, Perl, Python®, and PHP now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.