PHP Timezone incorrect When using time()

670 Views Asked by At

I am using time() to record the current time in my PHP application.

I am based in the UK so we use UTC timezone. This means that in the summer/winter we switch from GMT timezone to BST (which is GMT +1). The problem was that, time() still uses GMT timezone which means it is 1 hour behind. How do I make it so it shows the BST time which is 1 hour ahead of GMT?

When I echo out the time, it is 1 hour behind.

What I have Tried:

  • Adding the following pieces of code:

    date_default_timezone_set("Europe/London");

    ini_set("date.timezone", "Europe/London");

  • Re-Installing PHP 5.5 so the extensions storing timezones are updated.

Thanks

2

There are 2 best solutions below

0
On

Found my solution.

The problem is not with time() as that is just seconds past the date.

When coming to show the date, instead of using gmdate() instead use date().

This will take into account any default timezones you set in your document/ini file.

1
On

use gmdate ()

string gmdate ( string $format [, int $timestamp = time() ] )

php.net and Different results for date() and gmdate()