The PHP Mktime Function

Guest Author - Diane Cipollo

The built-in PHP mktime( ) function creates a Unix timestamp for a specific date and time.

You can use PHP to easily find the current, local time and date but you do not always want to work with the current or actual time. In many cases, you will need to work with a specific, arbitrary date and time. To work with this specific time and date, you first use the built-in PHP mktime( ) function to create a Unix timestamp1 for that date and time. This timestamp is assigned to a variable that you can then make use of in your program. In the example below, I have used the day and time of my birthday this year (August 23, 2005, 10:11 am). Let's take a look at the code.

$tstamp = mktime(10, 11, 0, 8, 23, 2005);

As you can see, this function takes 6 integer arguments in the order of hour, minute, second, month, day, and year. Of course, the computer keeps time in 24 hours per day but it also handles the hours after 12 noon a little differently. From noon onward, the hours are 13 through 24. Thus, 14 would be 2 pm. The mktime( ) function can also handle values beyond the 24th hour. If I had set the hour argument to 26 in the example above, the timestamp would be for 2 am on August 24, 2005. If any of the arguments are omitted, the value for that argument is set to the local, current time on the server that is running the program. For example, if I ran a program at 4 pm and omitted the hour argument, the program would automatically use 16 for the hour argument. The example above would result in the timestamp value of 1124809860.

Now that you have the timestamp for a specific date and time and that timestamp is assigned to a variable of your choice ($tstamp in the example above), you can use this variable in many ways within your program.

1A timestamp is an integer that represents the number of seconds from the Unix Epoch (which is January 1, 1970, 00:00:00) and the date you wish to work with.