Solved

wrong time zone php ini

Posted on 2011-09-26
7
341 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to use embargo Date/time in php 29 39
Testing a string condition in PHP 6 22
curl SSL certificate unable to get local issuer certificate 15 70
hasing a url 16 28
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

809 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