[Previous] [Contents] [Next]

Creating timestamps with mktime( ) and gmmktime( )

To create a timestamp for a past or future date in the range December 13, 1901 through January 19, 2038, the mktime( ) and gmmktime( ) functions are defined:

int mktime(int hour, int minute, int second, int month, int day, int year
     [, int is_dst])
int gmmktime(int hour, int minute, int second, int month, int day, int year
     [, int is_dst])

Both create a timestamp from the supplied components; the parameters supplied to gmmktime( ) represent a GMT date and time, while the parameters supplied to mktime( ) represent the local time. This example creates a timestamp for 9:30 A.M. on June 18, 1998:

$aDate = mktime(9, 30, 0, 6, 18, 1998);

Both functions are reasonably tolerant of zero values, and both correctly handle values out-of-range, allowing scripts to add a quantum of time without range checking. If the components of a date are outside the range of dates the function is defined for, -1 is returned. The following example shows how 30 days can be added to a date and time:

$paymentPeriod = 30;  // Days
// generates a timestamp for 26 June 2002 by
// adding 30 days to 27 May 2002
$paymentDue =
   mktime(0, 0, 0, 5, 27 + $paymentPeriod, 2002);
// A different approach adds the appropriate number
// of seconds to the timestamp for 27 May 2002
$paymentDue = mktime(0, 0, 0, 5, 27, 2002)
  + ($paymentPeriod * 24 * 3600);

Both functions allow the supplied date to be interpreted as daylight savings time by setting the flag is_dst to 1.

The order of the arguments to these functions is unusual and easily confused. While the mktime( ) and gmmktime( ) functions are similar to the Unix mktime( ) function, the arguments aren't in the same order.

[Previous] [Contents] [Next]