Page 525 - Beginning PHP 5.3
P. 525

Chapter 16: PHP and the Outside World
                         Next comes checkDateOfBirth(), the callback function to check that the entered date is valid. As you
                         might expect, this uses PHP’s checkdate() function to do the checking. Because the element to be
                         checked contains multiple field values, HTML_QuickForm provides an associative array of the values,
                         keyed by field name. checkDateOfBirth() extracts these three values and passes them to
                         checkdate() for checking, passing checkdate()’s return value (true or false) back to the
                          calling code:

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

                         The last function, calculateDays(), is called when the form is processed. First it creates two
                         timestamps: $currentDate, which stores the current date and time; and $dateOfBirth, which stores
                         the entered birth date. Both timestamps are created using mktime(). The month, day, and year
                          components of the date of birth are extracted from the three-element associative array stored in the
                          form’s dateOfBirth field:

                               $currentDate = mktime();
                               $dateOfBirth = mktime( 0, 0, 0, $values[“dateOfBirth”][“M”],
                               $values[“dateOfBirth”][“d”], $values[“dateOfBirth”][“Y”] );
                         Now that both dates are stored in timestamps, it’s easy to work out the number of seconds between the
                         two, and hence the number of days:

                               $secondsOld = $currentDate - $dateOfBirth;
                               $daysOld = (int) ( $secondsOld / 60 / 60 / 24 );
                         Finally, the function displays the output to the visitor. First it calls PHP’s date() function to display the
                         visitor’s date of birth in a nice format, then it tells the visitor how old he is in days:

                               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>”;
                             Calculating the difference in days by dividing the difference in seconds by (60 * 60 * 24) is not entirely
                             accurate due to days not always being 24 hours long (at least in time zones that use daylight saving
                             time). However, it’s good enough for the purposes of this script.





                           DateTime: The Future of PHP Date/Time Handling

                           Starting with version 5.2, PHP introduced a couple of useful date and time handling classes:
                            ❑       DateTime  for storing and manipulating dates and times
                            ❑       DateTimeZone  for representing time zones






                                                                                                         487





                                                                                                      9/21/09   9:15:35 AM
          c16.indd   487
          c16.indd   487                                                                              9/21/09   9:15:35 AM
   520   521   522   523   524   525   526   527   528   529   530