Working with Timestamps
There are times when using your own date format is beneficial, but in most cases a timestamp is the best choice. Let's look at how PHP interacts with the timestamp date format.
Formatting Dates
In Lesson 1, "Getting to Know PHP," you used the date function to display the current date by passing a format string as the argument, such as in the following example:
The date displayed looks something like this:
echo date("j F Y H:i:s");
The optional second argument to date is a timestamp value of the date that you want to display. For example, to display the date when a timestamp first requires a 10-digit number, you could use the following:
12 November 2004 10:23:55
The list of format codes for the date function is shown in Table 9.1.
echo date("j F Y H:I:s", 1000000000);
Creating Timestamps
Don't worry; you don't have to count from January 1, 1970, each time you want to calculate a timestamp. The PHP function mktime returns a timestamp based on given date and time values.The arguments, in order, are the hour, minute, second, month, day, and year. The following example would assign $timestamp the timestamp value for 8 a.m. on December 25, 2001:
The Unix timestamp format counts from January 1, 1970, at midnight GMT. The mktime function returns a timestamp relative to the time zone in which your system operates. For instance, mktime would return a timestamp value 3,600 higher when running on a web server in Texas than on a machine in New York with the same arguments.
$timestamp = mktime(8, 0, 0, 12, 25, 2001);
![]() | Greenwich Mean Time To obtain timestamp values that are always relative to GMTthe time in London when there is no daylight saving time adjustmentyou use gmmktime instead of mktime. |
You can exploit this behavior as a way of performing date and time arithmetic. Consider the following example, which calculates and displays the date and time 37 hours after midday on December 30, 2001:
echo date("d/m/Y", mktime(12, 0, 0, 2, 29, 2003));
By simply adding a constant to one of the arguments in mktime, you can shift the timestamp value returned by that amount. The date and time display as follows:
$time = mktime(12 + 37, 0, 0, 12, 30, 2001);
echo date("d/m/Y H:i:s", $time);
The value returned in this example has correctly shifted the day, month, year, and hour values, taking into account the number of days in December and that December is the last month of the year.
01/01/2002 01:00:00
Converting Other Date Formats to Timestamps
If you have a date stored in a format like DD-MM-YYYY, it's a fairly simple process to convert this to a timestamp by breaking up the string around the hyphen character. The explode function takes a delimiter argument and a string and returns an array that contains each part of the string that was separated by the given delimiter.The following example breaks a date in this format into its components and builds a timestamp from those values:
For many date formats, there is an even easier way to create a timestampusing the function strtotime. The following examples all display the same valid timestamp from a string date value:
$date = "03-05-1974";
$parts = explode("/", $date);
$timestamp = mktime(12, 0, 0,
$parts[1], $parts[0], $parts[2]);
Note that in the last examples, the date format given is MM/DD/YYYY, not DD/MM/YYYY. You can find the complete list of formats that are acceptable to strtotime at www.gnu.org/software/tar/manual/html_chapter/tar_7l.
$timestamp = strtotime("3 May 04");
$timestamp = strtotime("3rd May 2004");
$timestamp = strtotime("May 3, 2004");
$timestamp = strtotime("3-may-04");
$timestamp = strtotime("2004-05-03");
$timestamp = strtotime("05/03/2004");
Getting Information About a Timestamp
You can use the date function to return part or all of the date that a timestamp represents as a formatted string, but PHP also provides the geTDate function, which returns useful values from a timestamp.Taking a single timestamp argument, geTDate returns an associative array that contains the indexes shown in Table 9.2.
Key | Description |
---|---|
seconds | Seconds, 059 |
minutes | Minutes, 059 |
hours | Hours, 023 |
mday | Day of the month, 031 |
wday | Day of the week, 06, where 0 is Sunday |
yday | Day of the year, 0365 |
mon | Month number, 012 |
year | Four-digit year number |
weekday | Full day name, SundaySaturday |
month | Full month name, JanuaryDecember |
Note that when getdate is called without a timestamp argument, it returns an array that contains the elements in Table 9.2 for the current time.
$now = getdate();
switch ($now[wday]) {
case 0: // Sunday
case 6: // Saturday
echo "It's the weekend";
break;
default: echo "It's a weekday";
}