Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP timezone issue | BST and GMT

I have developed a piece of application which records when certain records where modified and created, so basically we take use of the time() function to record when a change is saved.

I am in the UK so my time-zone has to be GMT. However in the UK we use DST so in the summer we are no longer in GMT but in BST.

How would I change the timezone to be using BST (which is GMT +1). I would like to declare it in my php file so that it is easily changed. Here is what I have at the moment:

date_default_timezone_set("UTC");

When I change it to:

date_default_timezone_set("BST");

I get a php error Timezone ID 'BST' is invalid, and when I change it to Europe/London, it still stays as GMT rather then BST

like image 870
Eclipse Avatar asked May 27 '15 09:05

Eclipse


2 Answers

You would be setting your timezone to Europe/London, which automatically transitions between GMT and BST at the appropriate dates. The timezone includes this information, that's basically the point of timezones (in the PHP sense).

The PHP manual handily includes exactly this as a sample:

$timezone = new DateTimeZone("Europe/London");
$transitions = $timezone->getTransitions();
print_r(array_slice($transitions, 0, 3));

Array
(
    [0] => Array
        (
            [ts] => -9223372036854775808
            [time] => -292277022657-01-27T08:29:52+0000
            [offset] => 3600
            [isdst] => 1
            [abbr] => BST
        )

    [1] => Array
        (
            [ts] => -1691964000
            [time] => 1916-05-21T02:00:00+0000
            [offset] => 3600
            [isdst] => 1
            [abbr] => BST
        )

    [2] => Array
        (
            [ts] => -1680472800
            [time] => 1916-10-01T02:00:00+0000
            [offset] => 0
            [isdst] => 
            [abbr] => GMT
        )

)
like image 173
deceze Avatar answered Sep 19 '22 00:09

deceze


The time() function returns a unix timestamp, which is always going to be based on UTC (which is the same as GMT). It would be invalid for it to be adjusted for BST, or for any other time zone.

There are other functions for working with local time, and for converting the unix timestamp to a string for display. That is where you use the Europe/London time zone.

BST is not valid, because it could stand for "Bangladesh Standard Time", or a handful of other time zones. In general, don't rely on time zone abbreviations.

like image 42
Matt Johnson-Pint Avatar answered Sep 18 '22 00:09

Matt Johnson-Pint