PHP DateTime DST

I also searched the PHP and StackOverflow manual, but didn’t really find the right answer to my question. My dates are stored in UTC. Now if I do this:

$date = new DateTime('2012-03-16 14:00:00', 'UTC'); $date->setTimezone('Europe/Budapest'); 

Will DateTime :: setTimezone () automatically set DST? So, if I format the string, will it output 15:00:00 in the summer and 16:00:00 in the winter? Or will I have to install DST manually?

A related question, if I want to get DST time independent time (which I can save), will there be a next job?

 $date = new DateTime('now', 'UTC'); $date->format('Ymd H:i:s'); 

Or is it better to use simple gmdate('Ymd H:i:s') ?

+4
source share
1 answer

Your guess is correct.

In addition, it would take you 10 seconds to verify this.

Edit:

The correct syntax is:

 $date = new DateTime('2012-03-16 14:00:00', new DateTimeZone('UTC')); 

Regarding your second question. The time zone passed in the DateTime constructor is the "reference" time zone.

You should still call → setTimeZone (new DateTimeZone ('UTC')) to format it in the UTC time zone, if the default time zone is also not set to UTC (date_default_timezone_set).

Next time, try a little harder to try things out before asking.

+5
source

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


All Articles