views:

430

answers:

4

Hi all,

PLATFORM: PHP & mySQL

I am storing the date+time in database in the following format: date("Y-m-d H:i:s"). An example of a value that I have in my DB is : 2010-01-05 07:36:33. In my script, I have set the timezone as date_default_timezone_set("America/Chicago");

THE PROBLEM:

I read about the UNIX_TIMESTAMP somewhere and I was using that in my query. The value of UNIX_TIMESTAMP on a date value from the DB, seems to be different from the strotime(DB date Value).

EXAMPLE:

Consider that one of the DB values for the date column in my DB is 2010-01-05 07:36:33 Now if I fetch this date in the following way:

$result = mysql_query("SELECT date, UNIX_TIMESTAMP(date) AS ut_date FROM table");
$row = mysql_fetch_row($result);

//The result of this is:
$row['date']    = 2010-01-05 07:36:33
$row['ut_date'] = 1262657193
strtotime($row['date']) gives 1262698593

For my further calculations within my application, I need to work with strtotime(date). I have many comparisons to do that way. My problem would have solved, had the UNIX_TIMESTAMP(date) was same as strtotime(date). One of the sample query that I need to work with, is:

$gap = 1; // time in minutes
$tm2 = date ("Y-m-d H:i:s", mktime (date("H"),date("i")-$gap,date("s"),date("m"),date("d"),date("Y")));
$target = strtotime($tm2);

$result2 = mysql_query("UPDATE table2 SET stat = 0 WHERE UNIX_TIMESTAMP(today_login_time) < $target ");

The above is giving me incorrect results. If I try to replace UNIX_TIMESTAMP with strtotime in the above query, it gives me an error as the function strtotime seems to be PHP function and not respective mySQL function. Is there a respective mySQL function for the strtotime ? How do I solve the above problem? The code to solve the above problem is highly appreciated.

Thank you in advance.

+4  A: 

Probably the timezones of your MySQL server and your php instance differ. e.g.

echo date('Y-m-d H:i:s T', 1262657193);

prints on my machine 2010-01-05 03:06:33 CET (note the CET timezone, that's UTC+1) while your MySQL server interprets the same unix timestamp as 2010-01-05 07:36:33

see http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html
and http://docs.php.net/datetime.configuration#ini.date.timezone

VolkerK
I did setup the timezone as date_default_timezone_set("America/Chicago"). So how can I solve this now? Thank you.
Devner
e.g. by setting the same timezone for the MySQL server, either globally (e.g. in its .ini file) or on a per session basis (`SET time_zone='America/Chicago'` should work). You can also always revert to the UTC timezone by using MySQL's UTC_TIMESTAMP() or simply sending the unix timestamp via UNIX_TIMESTAMP(), see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
VolkerK
Thanks for the info. So do I just use SET time_zone='America/Chicago'; at the top of the script to make this work? Will that be enough? I am not sure if the web host will give me the .ini file to tweak upon. So if that's something that I can manage via my coding in scripts, that will be great. Please let me know. Thank you.
Devner
Yes, you'd have to send the `SET time_zone...` statement via mysql_query() to the MySQL server. If that's really your best option I don't know.
VolkerK
I tried the following but it does not seem to work:SET time_zone = 'America/Chicago';I get:Parse error: syntax error, unexpected T_STRING in settings.php on line 135. Tried it by removing the semi-colon at the end of the string, but still the same error. I even used SET SESSION time_zone = '+0:00' for testing but still the same error. My PHP Version is 5.2.4 Am I doing something wrong?
Devner
It's a MySQL statement, not a php construct. `mysql_query("SET time_zone='America/Chicago'");`
VolkerK
+1: Ahh, I see. I used it and it stopped throwing the errors. Thanks much.
Devner
Hmm.. it's weird... I have set mysql_query("SET time_zone='America/Chicago'"); at the top of my script and I used the following insert statement: $result = mysql_query("INSERT INTO ts (ts) VALUES ( NOW() ) "); Instead of inserting the current Chicago time, it inserts my local time from my PC. Any guesses what may be wrong?
Devner
A: 

you don't need to convert the sql timestamp to unix for a where query. just use DATE_FORMAT it makes things much simpler:

 WHERE DATE_FORMAT(`today_login_time`, "%Y-%m-%d") < '.date('Y-m-d', $my_tsp)
antpaw
Thanks for the comment. What exactly is $my_tsp ? How do I implement your solution in my code?
Devner
well i didn't understand what your query is doing. but $my_tsp could be any string with a date that has this format YYYY-MM-DD.
antpaw
I am trying to update the stat to 0, in case the datetime in DB is more than 1 minute old. So I am calculating the current time, subtracting 1 minute from it (let's call this $target) and trying to check if there exists any datetime that has value less than the $target. Does that make sense? I will need to make sure that the difference is counted upto the last second accurately, so how would I modify your solution for this? Please let me know. Thank you.
Devner
+1  A: 

Since the difference is exactly 11.5 hours, it seems you're having issues with timezones.

Jordan Ryan Moore
Yes, date_default_timezone_set("America/Chicago") is the timezone within my script. How do I solve it now? Thanks.
Devner
A: 

The format date("Y-m-d H:i:s") does not accurately describe a date and time because it does not include a timezone. Since no timezone is provided, both the web server and database are using their timezones which are clearly different.

You should consider storing the dates in the database as a unix timestamp. A unix timestamp is just the number of seconds that have elapsed since the Unix Epoch (January 1 1970 00:00:00 GMT). That is not effected by timezones or daylight-savings. This way, you will only have to worry Timezones and daylight-savings when converting to an actual calendar day and time.

Lawrence Barsanti
Devner