Page 133 - Open Soource Technologies 304.indd
P. 133

Open Source Technologies



                   Notes         Two  more  date  functions  are  available:  gmmktime()  and  mktime().  Both  functions  create  a
                                 timestamp based on parameters passed when the function is called. The difference between
                                 the two functions is that gmmktime() treats the date/time parameters passed as a Greenwich
                                 Mean Time (GMT), while parameters passed to mktime() are treated as local time. The order of
                                 parameters is not very user friendly, as you can see in the prototype of the following function:

                                 timestamp mktime ( [$hour [, $minute [, $second [, $month [, $day [,
                                 ‘$year [, $is_dst]]]]]]])



                                                The particularly weird order of the parameters.

                                 All  parameters  are  optional.  If  any  parameter  is  not  included,  the  “current”  value  is  used,
                                 depending on the current date and time. The last parameter, is_dst, controls whether the date
                                 and time parameters that are passed to the function are DST-enabled or not. The default value
                                 for the parameter is -1, which signals PHP to determine for itself whether the date falls into the
                                 range when DST is observed. Here is an example:

                                       Example:  <?php
                                               /* mktime with a date outside the DST range */
                                               echo date(“Ymd H:i:s”, mktime(15, 16, 17, 1, 17, 2004)). “\n”;
                                               echo date(“Ymd H:i:s”, mktime(15, 16, 17, 1, 17, 2004, 0)). “\n”;

                                               echo date(“Ymd H:i:s”, mktime(15, 16, 17, 1, 17, 2004, 1)). “\n”;
                                               /* mktime with a date inside the DST range */

                                               echo date(“Ymd H:i:s”, mktime(15, 16, 17, 6, 17, 2004)). “\n”;
                                               echo date(“Ymd H:i:s”, mktime(15, 16, 17, 6, 17, 2004, 0)). “\n”;
                                               echo date(“Ymd H:i:s”, mktime(15, 16, 17, 6, 17, 2004, 1)).

                                               “\n\n”;

                                               ?>
                                 The first three calls “make” a timestamp for January 17, in which no DST is observed. Therefore,
                                 setting the $is_dst parameter to 0 has no effect on the returned timestamp. If it’s set to 1, though,
                                 the timestamp will be one hour earlier, as the mktime() function converts the DST time (which
                                 is always one hour ahead of non-DST). For the second set of mktime() calls, we use June 17 in
                                 which DST is observed. Setting the $is_dst parameter to 0 now makes the function convert the
                                 time from non-DST to DST and, thus, the returned timestamp will be one hour ahead of the
                                 result of the first and third calls. The output is

                                 20040217 15:16:17
                                 20040217 15:16:17
                                 20040217 14:16:17

                                 20040617 15:16:17
                                 20040617 16:16:17

                                 20040617 15:16:17


        128                               LOVELY PROFESSIONAL UNIVERSITY
   128   129   130   131   132   133   134   135   136   137   138