PHP date returning wrong time
        Posted  
        
            by 
                gargantaun
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by gargantaun
        
        
        
        Published on 2012-04-13T11:18:54Z
        Indexed on 
            2012/04/13
            11:30 UTC
        
        
        Read the original article
        Hit count: 293
        
The following script is returning the wrong time after I call date_default_timezone_set("UTC")
<?PHP   
    $timestamp = time();
    echo "<p>Timestamp: $timestamp</p>";
    // This returns the correct time
    echo "<p>". date("Y-m-d H:i:s", $timestamp) ."</p>";
    echo "<p>Now I call 'date_default_timezone_set(\"UTC\")' and echo out the same timestamp.</p>";
    echo "Set timezone = " . date_default_timezone_set("UTC");
    // This returns a time 5 hours in the past
    echo "<p>". date("Y-m-d H:i:s", $timestamp) ."</p>";
?>
The timezone on the server is BST. So what should happen is that the second call to 'date' should return a time 1 hour behind the first call. It's actually returning a time 5 hours behind the first one.
I should note that the server was originally set up with the EDT timezone (UTC -4). That was changed to BST (UTC +1) and the server was restarted.
I can't figure out if this is a PHP problem or a problem with the server.
© Stack Overflow or respective owner