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

Unit 8: Working with Strings, Date and Time



             2004-12-30 (53 2004, Thursday)                                                       Notes
             2004-12-31 (53 2004, Friday)

             2005-01-01 (53 2004, Saturday)
             2005-01-02 (53 2004, Sunday)
             2005-01-03 (01 2005, Monday)
             2005-01-04 (01 2005, Tuesday)
             2005-01-05 (01 2005, Wednesday)
             2005-01-06 (01 2005, Thursday)
             As you can see, the ISO year is different for January 1 and 2, 2005, because the first week
             (Monday to Sunday) only has two days.
             Questions
             1. Briefly explain how the ISO 8601 year format option.
             2. Give the output of script.

                          DST Issues:  Every  year  around  October,  at  least  10–25  bugs  are  reported
                          when a day is listed twice in somebody’s overview. Actually, the day listed
                          twice is the date on which DST ends, as you can see in the following example.
                  Example:   <?php

                               /* Start date for the loop is October 31th, 2004 */
                               $ts = mktime(0, 0, 0, 10, 31, 2004);

                               /* We loop for 4 days */
                               for ($i = 0; $i < 4; $i++) {
                               echo date (“Y-m-d (H:i:s)\n”, $ts);
                               $ts += (24 * 60 * 60); /* 24 hours */
                           }
                           ?>

                           When this script is run, you see the following output:
                               2004-10-31 (00:00:00)
                               2004-10-31 (23:00:00)
                               2004-11-01 (23:00:00)
                               2004-11-02 (23:00:00)

            The 31st is listed twice because there are actually 25 hours between midnight, October 31 and
            November 1, not the 24 hours that were added in our loop. You can solve the problem in one
            of two ways. If you pick a different time of day, such as noon, the script will always have the
            correct date:

                  Example:   <?php
                               /* Start date for the loop is October 29th, 2004 */

                               $ts = mktime(12, 0, 0, 10, 29, 2004);
                               /* We loop for 4 days */
                               for ($i = 0; $i < 4; $i++) {

                                             LOVELY PROFESSIONAL UNIVERSITY                                   135
   135   136   137   138   139   140   141   142   143   144   145