Home » Php » How to retrieve a unix timestamp from DateTime object within last hour before DST switchover in PHP?

How to retrieve a unix timestamp from DateTime object within last hour before DST switchover in PHP?

Posted by: admin July 12, 2020 Leave a comment

Questions:

There is an example code

<?php
$dt = new \DateTime();
$dt->setTimezone(new \DateTimeZone('Europe/London'));

$timestamp = 1351383400;    
echo "$timestamp \n";

$dt->setTimestamp($timestamp);
echo $dt->getTimestamp();

which outputs 2 different timestamps 1351383400 (Sun, 28 Oct 2012 01:16:40 +0100, right before DST switch) and 1351387000 (Sun, 28 Oct 2012 01:16:40 +0000, 1 hour later, after DST switch)

The question is how can I make the Timestamp getter to return me exactly the same integer that has been passed to the Timestamp setter 1 row before ?

PHP 5.3.6

How to&Answers:

Hope this helps:

    $dt = new DateTime();
    $dt->setTimezone(new \DateTimeZone('Europe/London'));
    $timestamp = 1181503727;
    echo $timestamp . '<br />';

    $dt->setTimestamp($timestamp);
    echo $dt->format('U'); 

output:
1181503727
1181503727