Page 522 - Beginning PHP 5.3
P. 522

Part III: Using PHP in Practice
                          span.required { display: none; }
                        </style>
                      </head>
                      <body>
                        <h1>How many days old are you?</h1>
                    <?php
                    require_once( “HTML/QuickForm.php” );
                    require_once( “HTML/QuickForm/Renderer/Tableless.php” );
                    $form = new HTML_QuickForm( “form”, “get”, “days_old.php”, “”,
                    array( “style” => “width: 30em;” ), true );
                    $form->removeAttribute( “name” );
                    $form->setRequiredNote( “” );
                    $options = array( format => “MdY”, “minYear” => 1902, “maxYear” =>
                    date(“Y”) );
                    $form->addElement( “date”, “dateOfBirth”, “Your date of birth”, $options );
                    $form->addElement( “submit”, “calculateButton”, “Calculate” );
                    $form->addGroupRule( “dateOfBirth”, “Please enter your date of birth”,
                    “required” );
                    $form->addRule( “dateOfBirth”, “Please enter a valid date”, “callback”,
                    “checkDateOfBirth” );

                    if ( $form->isSubmitted() and $form->validate() ) {
                      $form->process( “calculateDays” );
                    }

                    $renderer = new HTML_QuickForm_Renderer_Tableless();
                    $form->accept( $renderer );
                    echo $renderer->toHtml();

                    function checkDateOfBirth( $value ) {
                      return checkdate( $value[“M”], $value[“d”], $value[“Y”] );
                    }

                    function calculateDays( $values ) {
                      $currentDate = mktime();
                      $dateOfBirth = mktime( 0, 0, 0, $values[“dateOfBirth”][“M”],
                    $values[“dateOfBirth”][“d”], $values[“dateOfBirth”][“Y”] );
                      $secondsOld = $currentDate - $dateOfBirth;
                      $daysOld = (int) ( $secondsOld / 60 / 60 / 24 );
                      echo “<p>You were born on “ . date( “l, F jS, Y”, $dateOfBirth ) . “.</p>”;
                      echo “<p>You are “ . number_format( $daysOld ) . “ day” . ( $daysOld != 1 ?
                    “s” : “” ) . “ old!</p>”;
                    }

                    ?>
                      </body>
                    </html>
                Now run the days_old.php script by visiting its URL in your Web browser. You should see a form
                with three menus for choosing the month, day, and year of your date of birth. Enter your date of birth
                and click Calculate to display your age in days. Figure 16-2 shows the script in action.






              484





                                                                                                      9/21/09   9:15:33 AM
          c16.indd   484
          c16.indd   484                                                                              9/21/09   9:15:33 AM
   517   518   519   520   521   522   523   524   525   526   527