Link to home
Start Free TrialLog in
Avatar of Luey
LueyFlag for United States of America

asked on

wrong time zone php ini

Hello my timestamps in my database are off by 2 hours.  I was told by my service provider to change my php ini file to correct it.  I looked in the ini file and it already has date.timezone = "America/New_York".   That should be correct for Eastern Time Zone but my timestamps are 2 hours behind.  Any suggestions would be appreciated.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Show us a script that illustrates the problem.  Just echo date('c');

You can use date_default_timezone_set('America/New_York'); -- see if that helps.
Avatar of Luey

ASKER

when i echo date('c'); it shows 2011-09-26T18:50:51-04:00 that is the correct time.  However when i use timestamp in my mysql database it sets the time 2 hours behind when I insert a record.  I have it set to CURRENT_TIMESTAMP as the default.
ASKER CERTIFIED SOLUTION
Avatar of neorush
neorush

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of neorush
neorush

You can test to see if the error reporting is off or not by make a page with this on it:

<?php
error_reporting('E_ALL');
ini_set('display_errors', 1);
echo $test;

?>
<br />There should be an error above that says something like Notice: Undefined Index....

Open in new window

^oops....wrong thread...I wish EE let you delete posts....
You can install this and run it (substitute your own variables for the DB connection).  It will show that the MySQL CURRENT_TIMESTAMP and NOW() values are independent of the PHP settings.
http://www.laprbass.com/RAY_temp_luey.php

This page might be helpful.
http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html

I will update my article to include this information about MySQL
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
<?php // RAY_temp_luey.php
error_reporting(E_ALL);
echo "<pre>";

// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";

// LIVE DATABASE CREDENTIALS
require_once('RAY_live_data.php');

// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $errmsg <br/>";
    die('NO DATA BASE');
}
// IF WE GOT THIS FAR WE CAN DO QUERIES


// CHANGE THE PHP VALUES
date_default_timezone_set('GMT');

// GET THE MYSQL VALUES
$sql = "SELECT CURRENT_TIMESTAMP AS t, NOW() AS n";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}

// SHOW WHAT WE GOT
$row = mysql_fetch_assoc($res);
var_dump($row);


// CHANGE THE PHP VALUES
date_default_timezone_set('America/New_York');

// GET THE MYSQL VALUES
$sql = "SELECT CURRENT_TIMESTAMP AS t, NOW() AS n";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}

// SHOW WHAT WE GOT
$row = mysql_fetch_assoc($res);
var_dump($row);

Open in new window

You can also change the timezone per connection with the query:
SET time_zone = 'America/New_York';
This is not what I would recommend, but it might work for the moment until you can modify some code to use php's time()