Solved

wrong time zone php ini

Posted on 2011-09-26
7
345 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
  • 4
  • 2
7 Comments
 
LVL 109

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 500 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 109

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

839 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