Dates and time in PHP

Dates and time form an important part of many sophisticated PHP applications. From simply displaying the date for a user to validating dates and times in user-supplied input, PHP developers have a wealth of functions to solve all manner of problems.

Complexity of dates and time

Unfortunately, our modern date and time systems were not designed with simple mathematics in mind. Some of the complexities one must take into account when handling dates are that not all months have the same number of days, some years have an extra day, the day of the week does not correspond to a given day of the month, and dates can be expressed in many formats (2003-03-01, 1/3/03 or '1st of march, this year'). Some complexities with handling times include dealing with time zones, time changes due to daylight savings, and converting times into different units (convert 5:15 p.m. to seconds, for example).

To simplify the situation, UNIX operating systems (in particular) treat dates and times in terms of the number of seconds since 1/1/1970. This is often called 'seconds since the epoch' or a (UNIX) timestamp. This system allows developers to calculate dates and times using integers - significantly easier than subtracting or adding days, months and hours. Many of the PHP date and time functions make use of this system.

Convert dates and times to seconds

Converting dates and times to the number of seconds since the epoch with PHP is simple, as the following script shows:

On line 02, the script generates the current number of seconds since the epoch using the time() function. The next line does the same, but for midday on 1 January 2003. We do this by using the mktime() - or make time - function. The arguments run as follows: the hour, the number of minutes, the number of seconds, the month, the day of the month and finally the year.

Line 04 calculates the result with simple mathematics, and line 05 reports the result to the user. Line 06 divides $secs by the number of seconds in a day. Note the (int) in front of this calculation: this casts the result to an integer - that is, rounds it from a decimal to a natural number. Line 07 reports the result to the user.

Validating dates

When dealing with user-supplied data, it is important to verify that the date is valid. For example, a user may accidentally submit the date 31 February 2003, which is invalid because this date doesn't exist. PHP allows developers to validate dates. Consider the following:

Lines 02, 03 and 04 define local variables from a user-submitted form. Line 05 calls checkdate() to verify that the supplied data is valid. If it is, checkdate() returns true; otherwise, false.

Getting more from timestamps

PHP allows developers to convert UNIX timestamps to highly formatted, human-readable dates. Using the date() function, any number of different date and time representations can be created. For example:

Line 02 creates a human-readable date 'Wednesday, 1st January 2003'. date() formats the converted timestamp in this way based on the modifiers passed in the first argument. The modifier 'l' returns the day of the week, 'j' returns the day of the month, 'S' returns one of 'st', 'nd', 'rd' or 'th' depending on the day of the month, 'F' returns the month and 'Y' returns the year.

Line 03 creates a human-readable time of the format hours:minutes:seconds. The modifier 'G' returns hours in 24 hour format: 'i' is minutes and 's' is seconds. A second argument has been omitted and, as such, date() uses the current timestamp. For more information and details of other modifiers see http://www.php.net/manual/en/function.date.php.

Dates and times can be a complex issue, but by using established conventions and built-in PHP functions, developers can add simple but sophisticated functionality to their applications.

The screen was particularly good. It is bright and visible from most angles, however heat is an issue, particularly around the Windows button on the front, and on the back where the battery housing is located.

My first impression after unboxing the Q702 is that it is a nice looking unit. Styling is somewhat minimalist but very effective. The tablet part, once detached, has a nice weight, and no buttons or switches are located in awkward or intrusive positions.

Reproduction in whole or in part in any form or medium without express written permission of IDG Communications is prohibited. Copyright 2013 IDG Communications.
ABN 14 001 592 650. All rights reserved.

Contact Us

With over 25 years of brand awareness and credibility, Good Gear Guide (formerly PC World Australia), consistently delivers editorial excellence through award-winning content and trusted product reviews.