I'm trying to use a timestamp for mysql entries, however it's showing up as behind 20 hours.
When I type the following into mysql command line the correct time is displayed (it's currently 8:15 PM Eastern)
SELECT NOW();
When I submit something into my database using the following, everything is correct except the time is being displayed as 12:15 am.
date('m/d/Y,h:i:a');
The column in my table is varchar(20) if that matters.
I also tried to make sure it's set to eastern timezone by using but got an error saying incorrect timezone.
SET time_zone = 'America/New_York';
Does anyone have a clue why I'm off by 20 hours in my databse entries but when I use mysql command line to look up the time it's displayed correctly?
I'm assuming this is a PHP timezone issue. You should check what Timezone you have set in your PHP ini file.
[Date]
; Defines the default timezone used by the date functions
date.timezone = America/New_York
You can also set the timezone in your script:
ini_set('date.timezone', 'America/New_York');
Seems like you were trying to change the Timezone in MySQL. You should also check your timezone setting MySQL, just to make sure.
Related
I believe I'm going mad.
$expire = date('Y-m-d H:i:s', strtotime("+30 minutes") );
It works as expected when echoed, yet when I insert it into a custom table via the wordpress database class, into a datetime column it's showing current time minus 30 mins.
Am I mad?
Simply given the time frame you are working with, it makes me wonder if you are seeing a Standard/Daylight time conflict, assuming you observe daylight savings time where you are.
First, See if you experience the same issue with "+60 minutes" as a test. If it is then -60 in the database, then it may be a bug, however, if it is then the current time in the database, it may actually be a timezone issue.
Make sure all of your timezones are properly configured on your host OSes and in your database. If the database column is of the type "timestamp" then mysql converts it to UTC on storage, and back on retrieval, so a mis-configured timezone could cause a 1 hour offset there as well.
Hope This Helps!
All date and time functions are now dependent upon a correct timezone setting.
You can either do this in your script
date_default_timezone_set('America/Los_Angeles'); // for example
OR
Check your php.ini for this setting
date.timezone = UTC
And set it correctly for your specific timezone, here is a List of supported timezones which you will need either way
I currently use $curdate=date('Y-m-d H:i:s'); to enter a timestamp to my blog's MySQL.
The problem is that the timezone of my MySQL is 2 hours ahead. At least in the timesaving period (I don't know if it is going to be any different when the timesaving period is over).
How should I redefine $curdate so that it records correct time based on PST time?
You can set the timezone the PHP uses for the duration of the execution of your script with date_default_timezone_set().
If you need to do something in your own timezone later in the execution of you script, you can call it again to set it back.
Alternatively (better?), if you use the MySQL NOW() function in your query, the time entered into the database will be calculated by MySQL, according to it's own timezone.
I want to be independent from the timezone configured on a server so in a script I set the time zone like this:
mysql_query("SET time_zone = '".date_default_timezone_get()."';");
The server is currently configured to Europe/Moscow which currently is UTC+4
Then in a PHP site I select something from the database like this:
date_default_timezone_set('Europe/Berlin');
$sth = $dbh->prepare("SET time_zone = '".date_default_timezone_get()."';");
$sth->execute();
$sth = $dbh->prepare("SELECT * from logs WHERE time like '2011-06-1%'");
$sth->execute();
I am using Timestamp field type and not Datetime.
Not what I get displayed is a timestamp that is 2 hours too far in the future.
The mysql doc says:
Values for TIMESTAMP columns are
converted from the current time zone
to UTC for storage, and from UTC to
the current time zone for retrieval.
So this brings me to the 3 possible cases:
Storage conversion works, select does not: No - because then The timestamp would be 2 (or 1 in winter) hours too far in the past
Storage conversion does not work, but select does: No - because then i would see UTC which is -1 hours which is not the case
Storage conversion doesn't work, select conversion doesn't work: Looks just like it!
Now the timestamp I write into the database is constructed and written PHP side:
$hourprec = "Y-m-d H:00:00";
$hour = date($hourprec); // mysql compatible
...
REPLACE INTO logs (time,...) VALUES('".$hour."','"....
I can imagine that this makes problems with mysqls time conversion because it comes as a string and I should do FROM_UNIXTIME or something.
But shouldn't it work at least with the select then?
Am I missing something? How do I have to do it if I want to store and read timestamps correctly in UTC in a mysql database but read/write them in scripts that have different time zones?
The answer was quite trivial.
The approach above is just fine, MySql just didn't know ANY timezones.
You can test this with the command SET time_zone = 'UTC';
If you have the same problem as the questioner you should recieve the following error:
#1298 - Unknown or incorrect time zone: 'UTC'
This can be easily fixed with the following command:
mysql_tzinfo_to_sql /usr/share/zoneinfo/|mysql -u root mysql -p
I have recently changed my server time zone to America/New_York.
The problem is when I run my php/mysql script to add rows into the database with default: CURRENT_TIMESTAMP, it adds the date as tomorrow instead of today.
Then when tomorrow rolls over at midnight on my server, the dates go back to yesterday?
MySQL uses its own time zone settings and PHP uses its own time zone settings.
Make sure when you configure your PHP time zone settings, set the same time zone settings for MySQL as well.
Oh and by the way, Timestamp data type in MySQL doesn't store time zone information, If you supply timezone to MySQL when writing to this field, MySQL will convert it to UTC based on its timezone settings.
SELECT ##global.time_zone, ##session.time_zone; -- perform this in mysql console
If it gives incorrect result - change the mysql server timezone explicitly in config file:
http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html
I've run in to an issue whereby PHP and MySQL seem to disagree on which timezone they should be using when converting certain timestamps back to date and time.
The problem arises when I have a timestamp which I would like to convert back to a datetime format. PHP gives the date and time based on GMT being the timezone, but MySQL seems to think it is operating in GMT+1, and converts accordingly.
Simple reproduction
$original_date = '2009-08-31 23:59:59';
$timestamp = strtotime($original_date);
echo $timestamp; // Gives 1251763199
echo date('Y-m-d H:i:s', $timestamp); // PHP: 2009-08-31 23:59:59
echo db_field('SELECT FROM_UNIXTIME(1251763199)'); // MySQL: 2009-09-01 00:59:59
The timestamp given by PHP seems to be the correct one for the date given, assuming timezone is GMT. The result from MySQL would have been the correct one had we been running BST (timestamp given fell within GMT+1 at that time).
If I try the above with a $timestamp from today (1267640942), PHP and MySQL both seem to be happy to tell me that it is 2010-03-03 18:29:02 (both returning GMT).
What timezone is set on the servers?
I've checked the MySQL docs, which say that if my timezone is set to system than the OS will provide the timezone info. This appears to be the case at the moment:
mysql> SELECT ##global.time_zone, ##session.time_zone;
+--------------------+---------------------+
| ##global.time_zone | ##session.time_zone |
+--------------------+---------------------+
| SYSTEM | SYSTEM |
+--------------------+---------------------+
The default timezone on my web server is GMT. This is also the default system timezone on my database server (according to the date command run on the cl on each server).
foo#bar:/home/foo$ date
Wed Mar 3 18:45:02 GMT 2010
So according to the docs, my DB server should be running on GMT, which is what we want. Yet the query output given in my test scripts suggests that it's running in GMT+1.
I know there are a number of workarounds for this problem (all date arithmetic being done fully in either PHP or MySQL, etc) but I'd love to get to the bottom of what's causing this discrepancy so we can sort it out and prevent anyone else on the team from being tripped up by it.
Does anyone know if there's a very basic setting that I've over-looked here, or know what could be causing this discrepancy?
Thanks!
i use this methodology:
take care of PHP and leave mysql alone
it is recommended to set default timezone for php via date_default_timezone_set php function.
use a TIMESTAMP field type for keeping date record in mySql
then when you insert:
$sDate = date("Y-m-d H:i:s");
mysql_query("insert into `table` set `created_on` = '$sDate' ");
and when you select:
mysql_query("select `created_on` from `table` ");
$iTime = strtotime($aRow['created_on']);
you can always have access to global time using gmdate php function:
$iTime_Global = gmdate("U", $iTime);
the mysql timezone would have no effect in your application if you just take care of your PHP code. (that is made by timezone set)
I would suggest using UTC on your server and MySQL install and convert the timezone(s) to what ever you want. The conversion in MySQL and PHP are fairly simple.
http://www.w3schools.com/php/php_ref_date.asp (getTimezone and setTimezone)
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html