Hudzilla.org - the homepage of Paul Hudson
Contents > Functions > Working with Date and Time Wish List | Report Bug | About Me ]

4.5.4     Converting from components: mktime()

This is NOT the latest copy of this book; click here for the latest version.

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

It is a common tactic to store year, month, and day in separate variables in order to make comparison easier, and there is a special function for creating a Unix timestamp from date components: mktime().

Of all the functions in PHP, this one has perhaps the most unusual parameter ordering, so I would really recommend against trying to memorise the order in which they come - that is what this book and the online PHP manual are for! The order is this: hour, minute, second, month, day, year, Is_Daylight_Savings_Time. Note that "hour" should be in 24-hour clock time.

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

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

The only parameter there that might not make sense is the last one - this 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 really 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, we could just add 13 to the month parameter (currently 6), like this:

$unixtime = mktime(10, 30, 0, 19, 20, 2005, -1);

Clearly there are not 19 months in the year, so PHP will add one to the year value, subtract 12 from the months value, and calculate the date from there. Similarly you could add 9990 to the hours value and PHP will jump ahead by 416 days.

Author's Note: Do note that all the parameters, if less than 10, should not be expressed with a leading zero. The reason for this is that numbers with a leading zero are interpreted by PHP as being octal numbers, and this is likely to cause unforeseen results. For example, saying 08 for the eighth day will be interpreted in octal, and the highest single-digit value in octal numbers is 7, so PHP will set the number as 0.





<< 4.5.3 Converting to a string: date()   4.6 Mathematics >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
deathGod - 08 Sep 2008

no. everyone posted on different days. Paul has just used todays date as the date people have posted instead of storing the actual posting date in a database and calling it each time, which is what he would have to do if he wanted to have the actual date of posting displayed.

This was posted on the 18th June 2006

What's the date today? - 08 Sep 2008

Did everyone really post their comments on the same day?

Vu - 08 Sep 2008

< Definition:
< int mktime ( [int hour [, int minute [, int second [, int
< month [, int day [, int year [, int is_dst]]]]]]])
<
< Sample:
< So, to pass in 10:30pm on the 20th of June 2005, you
< would use mktime() like this:
<
< $unixtime = mktime(22, 30, 0, 20, 6, 2005, -1);

"20" and "6" should change the places according to above definition.

Else, the the resulted Unix timestamp would be 1154921400, which is equivalent to 2006-08-06 22:30:00, meaning PHP subtracts 12 from 20 to get August and increases the year by 1 to 2006.

Vu - 08 Sep 2008

< Definition:
< int mktime ( [int hour [, int minute [, int second [, int
< month [, int day [, int year [, int is_dst]]]]]]])
<
< Sample:
< So, to pass in 10:30pm on the 20th of June 2005, you
< would use mktime() like this:
<
< $unixtime = mktime(22, 30, 0, 20, 6, 2005, -1);

"20" and "6" should change the places according to above definition.

Else, the the resulted Unix timestamp would be 1154921400, which is equivalent to 2006-08-06 22:30:00, meaning PHP subtracts 12 from 20 to get August and increases the year by 1 to 2006.

A PHP User - 08 Sep 2008

hmm, yeah daylight savings time can be determined from the date, and it's relatively easy to do that (if the OS supports it) but on the long day of the year (start of daylight saving) there's two hours that only differ by the setting of the daylight saving indicator.

A PHP User - 08 Sep 2008

< Definition:
< int mktime ( [int hour [, int minute [, int second [, int
< month [, int day [, int year [, int is_dst]]]]]]])
<
< Sample:
< So, to pass in 10:30pm on the 20th of June 2005, you
< would use mktime() like this:
<
< $unixtime = mktime(22, 30, 0, 20, 6, 2005, -1);

Isn't it that "20" and "6" should change the places according to above definition?

So, I'm confused now. What is the proper way to place arguments to mktime? Is it h,m,s,month,day,year,dst or h,m,s,day,month,year,dst?

Guna - 08 Sep 2008

The information about mktime() will correct crazy date values( month = 19) is very informative.

three chapters on date function is good. but it will be more useful and informative, if you have explained, how to handle dates less than 1970-01-01.
Whenever i try to get timestamp of a date less than 1970-01-01 it gives on error.

Is any way to handle such sitution?



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is six plus three?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow