Prev | Current Page 376 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"


Suppose you were recently hired to create a new Web site for a movie producer. As
we all know, any serious producer uses Roman numerals to represent years, and the
product manager tells you that any date on the Web site must appear in this format.
Take a moment to think about this requirement because fulfilling it isn??™t as easy as it may
sound. Of course, you could look up a conversion table online and hard-code the values,
but how would you ensure that the site copyright year in the page footer is always up to
date? You??™re just about to settle in for a long evening of coding when you pause for a
moment to consider whether somebody else has encountered a similar problem. ???No
way,??? you mutter, but taking a quick moment to search PEAR certainly would be worth
the trouble. You navigate over and, sure enough, encounter Numbers_Roman.
For the purpose of this exercise, assume that the Numbers_Roman package has been
installed on the server. Don??™t worry too much about this right now because you??™ll
learn how to install packages in the next section. So how would you go about making
sure the current year is displayed in the footer? By using the following script:
// Make the Numbers_Roman package available
require_once("Numbers/Roman.php");
CHAPTER 11 ?–  PEAR 313
// Retrieve current year
$year = date("Y");
// Convert year to Roman numerals
$romanyear = Numbers_Roman::toNumeral($year);
// Output the copyright statement
echo "Copyright © $romanyear";
?>
For the year 2007, this script would produce the following:
Copyright ?© MMVII
The moral of this story? Even though you may think that a particular problem is
obscure, other programmers likely have faced a similar problem, and if you??™re fortunate
enough, a solution is readily available and yours for the taking.


Pages:
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388