Name

mktime()

Synopsis

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

It's common practice to store year, month, and day in separate variables in order to make comparison easier, and the mktime() function is used to reassemble the components into one Unix timestamp.

Of all the functions in PHP, this one has the most unusual parameter order: hour, minute, second, month, day, year, Is_Daylight_Savings_Time. Note that the hour should be in 24-hour clock time.

So, to pass in 10:30 p.m. on the 20th of June 2005, you would use mktime() like this:

    $unixtime = mktime(22, 30, 0, 6, 20, 2005, -1);

The only parameter that might not make sense is the last one, which is where you tell PHP whether daylight savings time (DST) should be in effect. If this seems odd to you—surely PHP should know whether DST was in effect?—consider the difficulties there are in calculating it. Each country enters DST at its own time, with some countries even having various times inside itself. Other countries, such as Germany, have only been using the DST system since 1980, which further complicates the matter. So, PHP gives you the option: pass 1 as the last parameter to have DST on, pass 0 to have it off, and pass -1 to let PHP take its best guess.

Using mktime() is a great way to do date arithmetic, as it will correct crazy dates quite well. For example, if we wanted to add 13 months to the function call above without having to figure out the new settings, ...

Get PHP in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.