PHP contains very useful date and time related functions. However when using these in practice, you should understand them carefully. Because wrong use of these functions, can cause very hard to find bugs in your applications.
One of the common mistake PHP developers does is, ill usage of gmmktime() and mktime() functions, without knowing their proper meaning.
First let’s look at what gmmktime and mktime does.
Both these functions returns Unix time stamp. Unix time stamp is number of seconds elapse since epoch (1970 January 01 00:00:00 hours in GMT time zone)
These functions have following signature:
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 ]]]]]]] )
As you can see above, these two have identical signatures and all arguments are optional.
So different lies in internal functionality
mktime function assumes, parameters stated to it self are in servers time zone and (internally) use server's time zone information to calculate time stamp.
gmmktime function assumes, parameters stated to it self are in GMT (standard) time zone and ignore servers time zone information when calculating time stamp.
One of the important concepts that you should remember here is, time stamp is time zone independent.
Let’s take an example to clear this out.
Consider the date (and time) 1970 January 02:00:00 (2 AM in the morning), and our sample server (who is running the PHP scripts) resides in GMT +1 time zone.
Following lines are executed on the sample server:
Below is the out put we will receive:
3600
7200
This is bit confusing at first glance (specially when you are not totally familiar with meaning of what these functions do). Lot of people expect to return something less by gmmktime() function than mktime() function, simply because GMT is behind the GMT + 1 time zone.
However, above results are correct. What actually happened while executing can be explained as below.
When mktime() function execute, it assume time values pass to it self are in server’s time zone. There fore in order to figure out time stamp, it first calculates the given time in GMT time zone (because all time stamps are time zone independent, by calculating them in GMT). So when given time converted to GMT, mktime() function receive new date and time as 1970 January 01 01:00:00 hours. In simplest words, when time is 2AM in GMT + 1 time zone, we all know it is 1AM in GMT. Time stamp of the 1970 January 01 01:00:00 is 3600 (One hour passed epoch). There fore mktime() returns 3600 as time stamp.
In contrast to that, when gmmktime() function execute, it assume time values pass to it self are in GMT time zone. There fore, it doesn’t require converting them to GMT time zone again before calculating the time stamp. So gmmktime() calculate time stamp for 1970 January 01 02:00:00, which is equal to 7200 (2 hours passed epoch).
Hope you understand the different now.
One of the common mistake PHP developers does is, ill usage of gmmktime() and mktime() functions, without knowing their proper meaning.
First let’s look at what gmmktime and mktime does.
Both these functions returns Unix time stamp. Unix time stamp is number of seconds elapse since epoch (1970 January 01 00:00:00 hours in GMT time zone)
These functions have following signature:
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 ]]]]]]] )
As you can see above, these two have identical signatures and all arguments are optional.
So different lies in internal functionality
mktime function assumes, parameters stated to it self are in servers time zone and (internally) use server's time zone information to calculate time stamp.
gmmktime function assumes, parameters stated to it self are in GMT (standard) time zone and ignore servers time zone information when calculating time stamp.
One of the important concepts that you should remember here is, time stamp is time zone independent.
Let’s take an example to clear this out.
Consider the date (and time) 1970 January 02:00:00 (2 AM in the morning), and our sample server (who is running the PHP scripts) resides in GMT +1 time zone.
Following lines are executed on the sample server:
print_r(mktime(2,0,0,1,1,1970));
print(“<br/>”);
print_r(gmmktime(2,0,0,1,1,1970);
Below is the out put we will receive:
3600
7200
This is bit confusing at first glance (specially when you are not totally familiar with meaning of what these functions do). Lot of people expect to return something less by gmmktime() function than mktime() function, simply because GMT is behind the GMT + 1 time zone.
However, above results are correct. What actually happened while executing can be explained as below.
When mktime() function execute, it assume time values pass to it self are in server’s time zone. There fore in order to figure out time stamp, it first calculates the given time in GMT time zone (because all time stamps are time zone independent, by calculating them in GMT). So when given time converted to GMT, mktime() function receive new date and time as 1970 January 01 01:00:00 hours. In simplest words, when time is 2AM in GMT + 1 time zone, we all know it is 1AM in GMT. Time stamp of the 1970 January 01 01:00:00 is 3600 (One hour passed epoch). There fore mktime() returns 3600 as time stamp.
In contrast to that, when gmmktime() function execute, it assume time values pass to it self are in GMT time zone. There fore, it doesn’t require converting them to GMT time zone again before calculating the time stamp. So gmmktime() calculate time stamp for 1970 January 01 02:00:00, which is equal to 7200 (2 hours passed epoch).
Hope you understand the different now.

2 comments:
Good explanation, thanks
Finally I understood. Thanks a lot.
Post a Comment