Solved

wrong time zone php ini

Posted on 2011-09-26
7
338 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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
MySQL's CURRENT_TIMESTAMP() will use the server time.  Since this is now the MySQL server doing the work, not PHP.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 6

Expert Comment

by:neorush
Comment Utility
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
Comment Utility
^oops....wrong thread...I wish EE let you delete posts....
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Multilanguage Database Design in MySQL 5 34
MYSQL simple update statement 3 32
.htaccess file settings 4 31
wordpress issue 2 15
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now