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)
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());
Format |
Description |
Returned value |
Day specifiers | ||
|
Day of month, two digits, with leading zeros |
|
|
Day of week, three letters |
|
|
Day of month, no leading zeros |
|
|
Day of week, full names |
|
|
Day of week, numeric, Monday to Sunday |
|
|
Suffix for day of month
(useful with specifier |
|
|
Day of week, numeric, Sunday to Saturday |
|
|
Day of year |
|
Week specifier | ||
|
Week number of year |
|
Month specifiers | ||
|
Month name |
|
|
Month number with leading zeros |
|
|
Month name, three letters |
|
|
Month number, no leading zeros |
|
|
Number of days in given month |
|
Year specifiers | ||
|
Leap year |
|
|
Year, four digits |
|
|
Year, two digits |
|
Time specifiers | ||
|
Before or after midday, lowercase |
|
|
Before or after midday, uppercase |
|
|
Hour of day, 12-hour format, no leading zeros |
|
|
Hour of day, 24-hour format, no leading zeros |
|
|
Hour of day, 12-hour format, with leading zeros |
|
|
Hour of day, 24-hour format, with leading zeros |
|
|
Minutes, with leading zeros |
|
|
Seconds, with leading zeros |
|
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:
DATE_ATOM
This is the format for Atom feeds. The PHP format is “Y-m-d\TH:i:sP” and example output is “2012-08-16T12:00:00+00:00”.
DATE_COOKIE
This is the format for cookies set from a web server or JavaScript. The PHP format is “l, d-M-y H:i:s T” and example output is “Thursday, 16-Aug-12 12:00:00 UTC”.
DATE_RSS
This is the format for RSS feeds. The PHP format is “D, d M Y H:i:s O” and example output is “Thu, 16 Aug 2012 12:00:00 UTC”.
DATE_W3C
This is the format for the World Wide Web Consortium. The PHP format is “Y-m-d\TH:i:sP” and example output is “2012-08-16T12:00:00+00:00”.
The complete list can be found at http://tinyurl.com/phpdates.
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.