PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Rene Kluwen   dutchclock   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example usage
Class: dutchclock
Shows current or given time as a Dutch sentence
Author: By
Last change:
Date: 22 years ago
Size: 3,316 bytes
 

Contents

Class file image Download
<?

/**************************************************************************
**
** example for PHP class: dutchclock
** That class is made is a learning aid for foreign students learning
** the Dutch language.
**
** Programmed and donated to the public domain
** by Rene Kluwen <rene.kluwen@chimit.nl>.
**
** Note:
** register_globals must be turned on for this example script to work properly.
**
***************************************************************************/


require("dutchclock.php");

// This function returns an optionlist with numbers between $min and $max included.
// If $default is given, that number will be selected by default.
function options($min, $max, $default = -1)
{
       
$res = "";
        for (
$i = $min; $i <= $max; $i++) {
               
$selected = (($i == $default) ? " selected" : "");
               
$res .= sprintf("<option value=\"%02d\"$selected>%02d</option>\n", $i, $i);
        }
        return
$res;
}

switch (
$cmd) {
case
"show":
       
// show analog clock
       
$clock = new dutchclock($time);
       
$clock->show();
        break;
default:
       
// set variables $hours, $minutes & $ampm when not given by GET or POST
       
if (empty($hours)) {
               
$hours = date("h");
        }
        if (empty(
$minutes)) {
               
$minutes = date("i");
        }
        if (empty(
$ampm)) {
               
$ampm = date("a");
        }

       
// recalculate $hours to military time (24 hour clock)
       
if ($ampm == "pm") {
               
$hours = ($hours % 12) + 12;
        }
        else {
                if (
$hours == 12) {
                       
$hours = "00";
                }
        }

       
// $time will be the string passed to the class constructor
       
$time = "$hours:$minutes";
       
// $utime will be $time, but usable as url parameter
       
$utime = urlencode($time);
       
// $tmphours is $hours, calculated back to a 12 hour clock
       
$tmphours = $hours % 12;
        if (
$tmphours == 0) $tmphours = 12;
       
$allhours = options(1, 12, $tmphours);
       
$allminutes = options(0, 59, $minutes);
       
$amselected = (($ampm == "am") ? " selected" : "");
       
$pmselected = (($ampm == "pm") ? " selected" : "");

       
// the following two lines are the core of this script
       
$clock = new dutchclock($time);
       
$dutchtime = $clock->dutchtime();

       
// and output the data in a page
       
echo <<<EOH
<html>
<head>
<title>Dutch Clock</title>
</head>
<body>
<p>
Dutch Clock
</p>
<p>
&nbsp;Military time: <b>
$time</b>.
</p>
<embed src="example.php?cmd=show&time=
$utime" quality=high loop=false pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width=150 height=150></embed>
<p>
In Dutch: <i>
$dutchtime</i>.
</p>
<p>
<form action="example.php" method="get">
Enter new time:
<select name="hours">
$allhours
</select>
:
<select name="minutes">
$allminutes
</select>
<select name="ampm">
<option value="am"
$amselected>am</option>
<option value="pm"
$pmselected>pm</option>
</select>
<input type="submit" value="Go!"/>
</form>
</p>
</body>
</html>
EOH;
        break;
}

?>