Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

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, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page