current timestamp = tomorrow - php

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

Related

Formatting MySQL / Server timestamp with php?

I have a created a timestamp in MySQL that changes when an account is updated by a users, and this timestamp is echoed on the page. However it is displaying the server time rather than my local time. I can't set the timezone in MySQL, I tried. What is another way to change this, and how can it be implemented?
The problem with timestamp datatype is that
MySQL converts TIMESTAMP values from the current time zone to UTC for
storage, and back from UTC to the current time zone for retrieval.
(This does not occur for other types such as DATETIME.) By default,
the current time zone for each connection is the server's time. The
time zone can be set on a per-connection basis. As long as the time
zone setting remains constant, you get back the same value you store.
If you store a TIMESTAMP value, and then change the time zone and
retrieve the value, the retrieved value is different from the value
you stored.
So, it does not really matter what your timezone setting in php is, you need to set either mysql's timezone on a session basis using
SET time_zone = timezone
command, or you need to store the timezone of the client along with the timestamp and adjus the value based on the stored timezone. I would use the latter approach, since a client technically can change timezones and if the client access the timestamp data from a different timezone, then different data will be returned by mysql.
When echoing from a database add in some "filters" seemed to do the trick
<?php echo $row['field name']; strtotime(date("Y-m-d", 1310571061)); ?>
Without the above code it displayed the timestamp as: 2016-02-09 00:00:00
With the above code it displays thus: Tuesday, February 09, 2016

Timezone conflict between PHP and Mysql server

I have an issue with the date creation. The php server settings for timezone :
by using the date_default_timezone_get() php function, refers to UTC whereas the MSQL query for timezone i.e ##system_time_zone is referring to MST. It is a shared server, so I will not be able to make changes in the my.conf file on the server.
I want the date to be in UTC format, How do I go about making changes on the mysql server so that the Now() function and CURRENT_TIMESTAMP returns date values wrt UTC.
If I understood you right you can try and set a timezone per session from client code
SET time_zone = timezone;
Depending on whether you have timezone info imported or not it may look like
SET time_zone = 'Etc/UTC';
or
SET time_zone = '+00:00';
Further reading:
MySQL Server Time Zone Support
Try fetching date from mysql query using UTC_TIMESTAMP() instead of NOW(), UTC_TIMESTAMP() will give you date in UTC.

PHP use CURRENT_TIMESTAMP from eastern time zone

I have a query that looks like:
$query = "INSERT INTO `SmsServ`.`SmsReceived` (
`Id` ,
`Body` ,
`DateReceived`
)
VALUES ( NULL , "Foo" , CURRENT_TIMESTAMP);"
The problem with that query is that I will like to have eastern time as the time stamp. I have tried calling:
date_default_timezone_set("America/New_York");
At the begining of my php script but that still gives an incorrect time stamp.
You did change the php time zone. So try to change the mysql time zone aswell(php and mysql time default zones differs from each other):
mysql> SET GLOBAL time_zone = 'America/New_York';
mysql> SET SESSION time_zone = 'America/New_York';
CURRENT_TIMESTAMP in this case is not generated by PHP, but by MySQL. Your query is asking MySQL to set the current timestamp based on MySQL's server time. As such you would need to configure MySQL to use the eastern time zone, not PHP.
One thing you might consider is to just use GMT for database timestamps and do timezone and daylight savings conversions in the application. That way you don't potentially have the issue of mixed zone timestamps in the database. Of course, if you don't think you would ever have need to use anything other than Eastern time zone in your app, then this might not be important for you.
Mysql is not concerned about which timezone is using ..
As per documentation you can set the timezone to be used....if you are using MySQL 4.1.3 or +
SET GLOBAL time_zone = timezone
or
SET time_zone = timezone
NOTE : TIMEZONE settings are not populated by default. You need to populate the tables related to timezones under `mysql database . oNly after the settings you can set the timezone.

mysql/php timezone issues

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.

MySQL + PHP Timezone Issue

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.

Categories