?
Solved

wrong time zone php ini

Posted on 2011-09-26
7
Medium Priority
?
354 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:Luey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36602450
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.
0
 

Author Comment

by:Luey
ID: 36602559
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.
0
 
LVL 6

Accepted Solution

by:
neorush earned 2000 total points
ID: 36665997
MySQL's CURRENT_TIMESTAMP() will use the server time.  Since this is now the MySQL server doing the work, not PHP.
0
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
LVL 6

Expert Comment

by:neorush
ID: 36670499
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

0
 
LVL 6

Expert Comment

by:neorush
ID: 36670818
^oops....wrong thread...I wish EE let you delete posts....
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36709291
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
http://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

0
 
LVL 6

Expert Comment

by:neorush
ID: 36709878
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()
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question