Date and Time Functions

To keep track of the date and time, PHP uses standard Unix timestamps, which are simply the number of seconds elapsed since midnight, January 1, 1970. To determine the current timestamp, you can use the time function:

echo time();

Because the value is stored as seconds, to obtain the timestamp for this time next week you would use the following, which adds 7 days × 24 hours × 60 minutes × 60 seconds to the returned value:

echo time() + 7 * 24 * 60 * 60;

If you wish to create a timestamp for a given date, you can use the mktime function. Its output is the timestamp 946684800 for the first second of the first minute of the first hour of the first day of the year 2000:

echo mktime(0, 0, 0, 1, 1, 2000);

The parameters to pass are, in order from left to right:

  • The number of the hour (0–23)

  • The number of the minute (0–59)

  • The number of seconds (0–59)

  • The number of the month (1–12)

  • The number of the day (1–31)

  • The year (1970–2038, or 1901–2038 with PHP 5.1.0+ on 32-bit signed systems)

Note

You may ask why you are limited to the years 1970 through 2038. Well, it’s because the original developers of Unix chose the start of the year 1970 as the base date that no programmer should need to go before! Luckily, because as of version 5.1.0 PHP supports systems using a signed 32-bit integer for the timestamp, dates from 1901 to 2038 are allowed on them. However, a problem even worse than the first comes about because the Unix designers also decided that nobody would be using Unix after about 70 years or so, and therefore believed they could get away with storing the timestamp as a 32-bit value—which will accommodate dates only up to January 19, 2038! This will create what has come to be known as the Y2K38 bug (much like the “millennium bug,” which was caused by storing years as two-digit values, and which also had to be fixed). We have to hope it will all be solved well before we get too close to that date.

To display the date, use the date function. This function supports a plethora of formatting options, enabling you to display the date any way you could wish. The format is as follows:

date($format, $timestamp);

The parameter $format should be a string containing formatting specifiers as detailed in Table 7-4, and $timestamp should be a Unix timestamp. For the complete list of specifiers, please see http://tinyurl.com/phpdatefuncs. The following command will output the current date and time in the format “Thursday April 15th, 2010 - 1:38pm”:

echo date("l F jS, Y - g:ia", time());
Table 7-4. The major date function format specifiers

Format

Description

Returned value

Day specifiers

d

Day of month, two digits, with leading zeros

01 to 31

D

Day of week, three letters

Mon to Sun

j

Day of month, no leading zeros

1 to 31

l

Day of week, full names

Sunday to Saturday

N

Day of week, numeric, Monday to Sunday

1 to 7

S

Suffix for day of month (useful with specifier j)

st, nd, rd, or th

w

Day of week, numeric, Sunday to Saturday

0 to 6

z

Day of year

0 to 365

Week specifier

W

Week number of year

01 to 52

Month specifiers

F

Month name

January to December

m

Month number with leading zeros

01 to 12

M

Month name, three letters

Jan to Dec

n

Month number, no leading zeros

1 to 12

t

Number of days in given month

28, 29, 30, or 31

Year specifiers

L

Leap year

1 = Yes, 0 = No

Y

Year, four digits

0000 to 9999

y

Year, two digits

00 to 99

Time specifiers

a

Before or after midday, lowercase

am or pm

A

Before or after midday, uppercase

AM or PM

g

Hour of day, 12-hour format, no leading zeros

1 to 12

G

Hour of day, 24-hour format, no leading zeros

1 to 24

h

Hour of day, 12-hour format, with leading zeros

01 to 12

H

Hour of day, 24-hour format, with leading zeros

01 to 24

i

Minutes, with leading zeros

00 to 59

s

Seconds, with leading zeros

00 to 59

There are a number of useful constants that you can use with the date command to return the date in specific formats. For example, date(DATE_RSS) returns the current date and time in the valid format for an RSS feed. Some of the more commonly used constants are:

The complete list can be found at http://tinyurl.com/phpdates.

Using checkdate

You’ve seen how to display a valid date in a variety of formats. But how can you check whether a user has submitted a valid date to your program? The answer is to pass the month, day, and year to the checkdate function, which returns a value of TRUE if the date is valid, or FALSE if it is not.

For example, if February 30 of any year is input, it will always be an invalid date. Example 7-3 shows code that you could use for this. As it stands, it will find the given date invalid.