Search code examples
phpdatetimetimezoneepoch

PHP timestamp an hour slow


I am working on an ancient system written in vanilla PHP.

I am having some time conversion issues.

In the below example, I an expecting both the timestamp and the "pretty date" to reflect one another:

<?php
date_default_timezone_set( 'Europe/London' );

$timestamp = 1509062400; //Friday, October 27, 2017 12:00:00 AM

$date = new DateTime();
$date->setTimezone( new DateTimeZone( 'Europe/London' ) );
$date->setTimestamp( $timestamp + 3601 );
$date->setTime( 0, 0 );

echo $date->getTimestamp();
echo '<br/>';
echo $date->format( 'Y-m-d H:i:s' );

However, when running this script on the server, the result is the following:

Timestamp: 1509058800
Pretty Date: 2017-10-27 00:00:00

The pretty date represents 27th Oct 00:00, but the timestamp (according to epochconvertor) represents 26th Oct 23:00.

I am not sure why the timestamp is an hour slow. Can anyone help?


Solution

  • If you're in London, then your timestamp is likely in British Summer Time. The epoch is going to be in GMT. BST is one hour ahead of GMT.