13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

478 Chapter 21 Managing the Date <strong>and</strong> Time<br />

Listing 21.1<br />

$year = 1972;<br />

Continued<br />

// remember you need bday as day month <strong>and</strong> year<br />

$bdayunix = mktime (0, 0, 0, $month, $day, $year); // get ts for then<br />

$nowunix = time(); // get unix ts for today<br />

$ageunix = $nowunix - $bdayunix; // work out the difference<br />

$age = floor($ageunix / (365 * 24 * 60 * 60)); // convert from seconds to years<br />

echo “Age is $age”;<br />

?><br />

This script sets the date for calculating the age. In a real application, it is likely that this<br />

information might come from an HTML form.The script begins by calling mktime() to<br />

work out the timestamp for the birthday <strong>and</strong> for the current time:<br />

$bdayunix = mktime (0, 0, 0, $month, $day, $year);<br />

$nowunix = time(); // get unix ts for today<br />

Now that these dates are in the same format, you can simply subtract them:<br />

$ageunix = $nowunix - $bdayunix;<br />

Now, the slightly tricky part: converting this time period back to a more human-friendly<br />

unit of measure.This is not a timestamp but instead the age of the person measured in<br />

seconds.You can convert it back to years by dividing by the number of seconds in a year.<br />

You then round it down by using the floor() function because a person is not said to<br />

be, for example, 20, until the end of his twentieth year:<br />

$age = floor($ageunix / (365 * 24 * 60 * 60)); // convert from seconds to years<br />

Note, however, that this approach is somewhat flawed because it is limited by the range<br />

of Unix timestamps (generally 32-bit integers). Birthdates are not an ideal application<br />

for timestamps.This example works on all platforms only for people born from 1970<br />

onward.Windows cannot manage timestamps prior to 1970. Even then, this calculation<br />

is not always accurate because it does not allow for leap years <strong>and</strong> might fail if midnight<br />

on the person’s birthday is the daylight savings switchover time in the local time zone.<br />

Calculating Dates in <strong>MySQL</strong><br />

<strong>PHP</strong> does not have many date manipulation functions built in. Obviously, you can write<br />

your own, but ensuring that you correctly account for leap years <strong>and</strong> daylight savings time<br />

can be tricky.Another option is to download other people’s functions.You can find many as<br />

user-contributed notes in the <strong>PHP</strong> manual, but only some of them are well thought out.<br />

Note<br />

Several date calculation functions have been added to <strong>PHP</strong> 5.3, including date_add(), date_sub(),<br />

<strong>and</strong> date_diff() among others. These date manipulation functions eliminate having to use <strong>MySQL</strong> to<br />

provide the easy date manipulation that <strong>PHP</strong> previously lacked.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!