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.

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

Because no specific built-in function is available for such calculations, an SQL query<br />

to calculate the exact number of years is fairly complex. Here, we took a shortcut <strong>and</strong><br />

divided the age in days by 365.25 to give the age in years.This calculation can be one<br />

year out if run on somebody’s birthday, depending on how many leap years there have<br />

been in that person’s lifetime.<br />

Using Microseconds<br />

For some applications, measuring time in seconds is not precise enough to be useful. If<br />

you want to measure very short periods, such as the time taken to run some or all of a<br />

<strong>PHP</strong> script, you need to use the function microtime().<br />

In <strong>PHP</strong> 5, you should pass true to microtime().When this optional parameter is<br />

provided, microtime() will return the time as a floating point value that is ready for<br />

whatever use you have in mind.The value is the same one returned by mktime(),<br />

time(), or date() but has a fractional component.<br />

The statement<br />

echo number_format(microtime(true), 10, '.', '');<br />

produces something like 1174091854.84.<br />

On older versions, you cannot request the result as a float. It is provided as a string. A<br />

call to microtime() without a parameter returns a string of this form "0.34380900<br />

1174091816".The first number is the fractional part, <strong>and</strong> the second number is the number<br />

of whole seconds elapsed since January 1, 1970.<br />

Dealing with numbers rather than strings is more useful, so in <strong>PHP</strong> 5 it is easiest to<br />

call microtime() with the parameter true.<br />

Using the Calendar Functions<br />

<strong>PHP</strong> has a set of functions that enable you to convert between different calendar systems.The<br />

main calendars you will work with are the Gregorian, Julian, <strong>and</strong> Julian Day<br />

Count.<br />

Most Western countries currently use the Gregorian calendar.The Gregorian date<br />

October 15, 1582, is equivalent to October 5, 1582, in the Julian calendar. Prior to that<br />

date, the Julian calendar was commonly used. Different countries converted to the<br />

Gregorian calendar at different times <strong>and</strong> some not until early in the twentieth century.<br />

Although you may have heard of these two calendars, you might not have heard of<br />

the Julian Day Count (JD). It is similar in many ways to a Unix timestamp. It is a count<br />

of the number of days since a date around 4000 BC. In itself, it is not particularly useful,<br />

but it is useful for converting between formats.To convert from one format to another,<br />

you first convert to a Julian Day Count <strong>and</strong> then to the desired output calendar.<br />

To use these functions under Unix, you first need to compile the calendar extension<br />

into <strong>PHP</strong> with --enable-calendar.These functions are built into the st<strong>and</strong>ard<br />

Windows install.

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

Saved successfully!

Ooh no, something went wrong!