📜 ⬆️ ⬇️

PHP: problem with timestamp in datetime

Many PHP developers use DateTime objects. I will not write about its convenience, especially on Habré already there was an article about this class , this is not the point.
All is good, yes, this class has a problem, which I encountered only recently.

The 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 included in 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, which are described in the documentation for the strtotime function. You can also pass a timestamp to the constructor by placing it after the "@" symbol, for example, like this:
')
$ date = new DateTime ( "@ 1232488800" );

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

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

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

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

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

$ 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.)

Wonderful. Now we have 2 identical time stamps and matching dates.

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

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

and what we see:

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

The time stamp remains the same, the time zone also has not changed, but the date and time have shifted by 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 with bewilderment and could not understand how this was possible. I revised the documentation again - I did it all right.

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

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

and, O MIRACLE! The script gave me the desired date.

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

I did all these movements in Vindovs, began to sin on it, but having tried it in Linkus, and having obtained the same result, I look towards the developers of the language. The PHP version in both axes is 5.2.4, so perhaps it has already been fixed in newer versions. But who warned - that is armed.

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

Source: https://habr.com/ru/post/49479/


All Articles