PHP: problem with timestamp in DateTime

    Many PHP developers use DateTime class objects. I won’t write about its conveniences, all the more so on Habré there was already an article about this class , this is not about that.
    Everything would be fine, but this class has a problem that I encountered only recently.

    An object of the DateTime class in PHP can be created as a standard method:

    $ date = new DateTime ("2009-01-21");

    as well as the date_create function, which has been part of PHP since version 5.2.0:

    $ date = date_create ("2009-01-21");

    The constructor takes as a parameter a string that defines the date and time, it can also be the names of the days of the week and a bunch of other parameters that are described in the documentation for the strtotime function. It is still possible to pass a timestamp to the constructor by placing it after the "@" character, for example, like this:

    $ date = new DateTime ("@ 1232488800");

    It is with this parameter that some confusion arose.
    To display information about a specific date object, create the following function:

    function date_info (DateTime $ date) {
        echo "
    ";
        echo “Timestamp:”. $ date-> format (“U”). ' - '. $ date-> format ("d / m / Y H: i: s"). "
    ";
        $ tz = $ date-> getTimezone ();
        echo "Timezone:". $ tz-> getName (). "(Offset:". $ date-> getOffset (). "sec.)
    ";
    }

    In it, we display the following information:
    - time stamp
    - date and time itself
    - time zone
    - offset in seconds of this same time zone

    Create date objects and display information about it

    $ date1 = new DateTime ("2009-01-21 00 : 00: 00 ");
    date_info ($ date1);

    got the

    Timestamp: 1232488800 - 01/21/2009 00:00:00
    Timezone: Europe / Helsinki (Offset: 7200 sec.)

    ok, we have a label, date and time zone.
    Let's create another object, pass the week name of the same day - “Wednesday” to it in the parameter.

    $ date2 = new DateTime ("Wednesday");
    date_info ($ date2);

    Since this is the nearest Wednesday, the date will be the same

    Timestamp: 1232488800 - 01/21/2009 00:00:00
    Timezone: Europe / Helsinki (Offset: 7200 sec.)

    Great. Now we have 2 identical timestamps and matching dates.

    Now, attention, create an object with a timestamp parameter and display information about it

    $ date3 = new DateTime ("@ 1232488800");
    date_info ($ date3);

    and what do we see:

    Timestamp: 1232488800 - 01/20/2009
    22:00:00 Timezone: Europe / Helsinki (Offset: 7200 sec.)

    The time stamp remained the same, the time zone also did not change, but the date and time shifted to the time corresponding to the time zone offset, only with a minus sign. That is, the displacement as such did not work for us.

    I looked at the result in bewilderment and could not understand how this is possible. I revised the documentation again - it seems I did everything right.

    Everything was decided, as usual, by the method of scientific poking. I thought, why not apply the current time zone

    $ date-> setTimezone (new DateTimeZone ("Europe / Helsinki") to the object ;
    date_info ($ date);

    and, OH MIRACLE! The script gave me the right date.

    Timestamp: 1232488800 - 01/21/2009 00:00:00
    Timezone: Europe / Helsinki (Offset: 7200 sec.)

    I did all these gestures in Windows, I began to sin on it, but having tried in Linkus, and getting the same result, I scamble in the direction of the language developers. The PHP version in both OS 5.2.4, so that, perhaps, in newer versions is already fixed. But whoever is warned is armed.

    PS I tried to set the time zone for the rest of the date objects, but it did not bring any changes, so I did not describe this.

    Also popular now: