PHP timezone issue | BST and GMT
Asked Answered
R

5

10

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

Rozier answered 27/5, 2015 at 9:8 Comment(1)
what about date_default_timezone_set('Etc/GMT-1');Oversell
B
11

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
        )

)
Bessie answered 27/5, 2015 at 9:18 Comment(6)
As stated, i have changed it to europe/london but it is still using GMT rather then BST (its one hour behind)Rozier
Then you probably need to update your timezone db. If it's out of date and there's been a change in timezone definitions in the real world your PHP won't have this info.Bessie
Can you please shed some insight as to where i should insert those files inside the timezone db into? thanksRozier
See #3564978. You should use PECL to update the timezonedb extension. However, depending on how you installed PHP, there may be better ways. For example, you should maybe use the package manager that installed PHP to begin with to update your PHP version overall. This is too broad to answer correctly in a comment.Bessie
Nothing has changed with Europe/London in quite a long time.Heins
@Rozier Then it would indeed be time to clarify how exactly you think your current timezone handling is problematic. What do you expect, and what do you get where/when/how?Bessie
H
1

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.

Heins answered 27/5, 2015 at 15:20 Comment(0)
A
1
date_default_timezone_set("Europe/London");
echo date('Y-m-d H:i:s');

Use "Europe/London" instead of BST/GMT/UTC with date_default_timezone_set(), probably the right way to handle time for an instance. This way, the timezone time is preserved throughout the time the page instance is active.

Appendicular answered 27/5, 2022 at 10:53 Comment(0)
L
0

There is an issue that Europe/London time-zone can be wrong by +/- 1 hour from the actual time in the real world in the UK .

$var = time();

See this example:

print date_default_timezone_get();
print "\n";
print date('r',1698136637);

By default PHP uses UTC time so this would be:

UTC
Tue, 24 Oct 2023 08:37:17 +0000

And through third party software for our records this is the correct time the date was set.

However, using Europe/London there is an issue:

date_default_timezone_set('Europe/London');
print date_default_timezone_get();
print "\n";
print date('r',1698136637);

Which gives:

Europe/London
Tue, 24 Oct 2023 09:37:17 +0100

This is not the correct time, as the time this record was made IN LONDON in the UK , the time was 8:37am.

Solution: use UTC rather than Europe/London time zones.

Ladd answered 24/10, 2023 at 9:49 Comment(0)
S
0

I'm using this for my literary clock at bigjobby.com/time/.

<?php

// Set the default timezone to UK
date_default_timezone_set('Europe/London');

// Get the current date and time
$now = new DateTime();

echo $now->format('Y-m-d H:i:s') . "\n";

// If you want to display the timezone
echo $now->getTimezone()->getName();
?>
Sanguine answered 4/4 at 19:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.