PHP DateTime Timezones - Constructor vs Setter method

前端 未结 1 391
灰色年华
灰色年华 2021-01-20 00:53

When using the PHP DateTime class and try to set a DateTimeZone I get different results depending on how I set it: using the DateTime::__cons

相关标签:
1条回答
  • 2021-01-20 01:57

    This is normal behaviour.

    When you don't specify the timezone in the constructor, the default timezone is used, i.e what was set using date_default_timezone_set().

    When you then call:

    $dateTimeTwo->setTimezone(new DateTimeZone('America/Los_Angeles'));
    

    It moves the date set in the default timezone into the new timezone.


    1) (constructor) set date in 'America/Los_Angeles'
    2) (setter) set date in default timezone, move date to 'America/Los_Angeles'


    Your default timezone was probably UTC or something close. You told the computer to set 2014-08-01 at UTC. You then asked to switch to the 'America/Los_Angeles' timezone which is 7 hours earlier thus by changing the date to 2014-07-31 at 17:00.

    0 讨论(0)
提交回复
热议问题