Using PHP DateTime to parse and convert PST time to PDT (GMT -8 - GMT -7)

It’s difficult for me to use PHP DateTime convert the date received with the GMT -8 (PST) time zone to a human-readable format with the GMT -7 (PDT) time zone.

Here is an example:

 $tz = new DateTimeZone('America/Los_Angeles'); $saleEndDate = new DateTime("2016-11-07T17:30:00-08:00"); $saleEndDate->setTimezone($tz); echo $saleEndDate->format('Ymd H:i:s'); 

The output of the above code: 2016-11-07 17:30:00 . However, it should display 2016-11-07 18:30:00 because America/Los_Angeles now in daylight (GMT -7, PDT).

From what I read in DateTime Docs , the new DateTime command should be able to interpret that line 2016-11-07T17:30:00-08:00 has a GMT -8 time zone:

The time zone parameter and the current time zone are ignored when the time parameter either contains a UNIX time stamp (for example, 946684800) or indicates the time zone (for example, 2010-01-28T15: 00: 00 + 02 :. 00)

However, I don't think DateTime correctly recognizes GMT -8.

Does anyone know which approach is needed for proper conversion between time zones?

Update:

I also tried passing in DateTimeZone as a second parameter to the DateTime constructor, but also to no avail:

 $tz = new DateTimeZone('America/Los_Angeles'); $saleEndDate = new DateTime("2016-11-07T17:30:00-08:00", new DateTimeZone("America/Los_Angeles")); $saleEndDate->setTimezone($tz); echo $saleEndDate->format('Ymd H:i:s'); 

Also does not work:

 $tz = new DateTimeZone('America/Los_Angeles'); $saleEndDate = new DateTime("2016-11-07T17:30:00", new DateTimeZone("PST")); $saleEndDate->setTimezone($tz); echo $saleEndDate->format('Ymd H:i:s'); 

Also does not work:

 $tz = new DateTimeZone("PDT"); $saleEndDate = new DateTime("2016-11-07T17:30:00", new DateTimeZone("PST")); $saleEndDate->setTimezone($tz); echo $saleEndDate->format('Ymd H:i:s'); 
+5
source share
2 answers

Not the biggest, but this is the only way I can do this.

 $tz = new DateTimeZone('America/Los_Angeles'); $saleEndDate = new DateTime("2016-11-07T17:30:00-08:00"); $saleEndDate->setTimezone($tz); $stamp = $saleEndDate->format('U'); $zone = $tz->getTransitions($stamp, $stamp); if(!$zone[0]['isdst']) $saleEndDate->modify('+1 hour'); echo $saleEndDate->format('Ymd H:i:s'); 

What I'm doing here, use the DateTimeZone :: getTransitions function to determine if the date you specified is DST or not. If this is not the case, add one hour. Note that this does not change the time zone , it just adjusts the DST shift

Here you can see it.

+2
source

DateTime works as it should. If you are not in an area that has observed various DSTs in a larger area, America/Los_Angeles left DST (PDT-> PST) on November 6th (in 2016).

https://www.timeanddate.com/news/time/usa-canada-end-dst-2016.html

In timezeonedb you can view the dates it uses by doing an array search for your specific date / time (which Machavity did) and getting the fact that it is not in DST, and then continuing to change it manually. This is not an answer, as it will ultimately fail unless you manually add a cutoff time for manual correction to stop.

Checking the transition date around your date shows:

 date_default_timezone_set('America/Los_Angeles'); $theDate = new DateTime("2016-11-07T17:30:00",new DateTimeZone("America/Los_Angeles")); $theDateBefore = new DateTime("2016-03-01"); $theDateAfter = new DateTime("2017-03-15"); echo "<pre>"; print_r( $theDate->getTimezone()->getTransitions( $theDateBefore->getTimestamp(),$theDateAfter->getTimestamp())); echo "</pre>"; 

the result is an array of 4:

 Array ( [0] => Array ( [ts] => 1456819200 [time] => 2016-03-01T08:00:00+0000 [offset] => -28800 [isdst] => [abbr] => PST ) [1] => Array ( [ts] => 1457863200 [time] => 2016-03-13T10:00:00+0000 [offset] => -25200 [isdst] => 1 [abbr] => PDT ) [2] => Array ( [ts] => 1478422800 [time] => 2016-11-06T09:00:00+0000 [offset] => -28800 [isdst] => [abbr] => PST ) [3] => Array ( [ts] => 1489312800 [time] => 2017-03-12T10:00:00+0000 [offset] => -25200 [isdst] => 1 [abbr] => PDT ) ) 

Array [0] is a time zone that acts like theDateBefore , and you can see that date changes apply to your time.

Your sale date falls after changing from PDT to PST.

To return the correct date / time code, you need to manually change it. The implementation of this method, which was adopted, would give false results. As I mentioned, you will need to surround this with what you want the user timezone to be enabled.

+1
source

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


All Articles