Maye I am wrong to use now() to get timestamps when storing data?
When I display date/time to the user, of course he wants to see a local time, and if he inputs time related data, rather than me using now() then he will be inputting local date/time.
Why code is getting muddled with conversations - what's the best practice for handling timestamps? UTC/locla time? How & when to adjust?
Just store all the dates in timestamp mysql column type, which can handle all timezone issues, and mysql will do all work for you.
So in the begin of your application startup all you need is to specify what timezone you need the dates belongs towith query:
SET time_zone='Asia/Vladivostok'
For example.
Also, in this case you should not get any timestamps from php, if you need to insert current time - you have to use mysql's NOW().
That's all.
Store all the dates in one time zone so they're consistent. UTC/GMT+0 is good for this.
Then use CONVERT_TZ to convert input to UTC/GMT+0 or from UTC/GMT+0 to a user's time zone.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Since all you've asked about are SQL functions, you should tag the question with the RDBMS you're using (MySQL?) instead of PHP.
Related
I am currently studying the best way to handle timezones on my website. People from many different countries will access it simultaneously, and I have to show them time-based information, so I thought:
Store every time on database according to my server (same timezone, defined by PHP)
Then, the user has the option to choose his timezone, and I do the needed conversions by using mysql function DATEADD.
This seems to work fine, but my questions are:
Is this the best way?
Is DATEADD the most efficient function to handle the hour difference?
Thanks.
As described in MySQL Server Time Zone Support:
The current session time zone setting affects display and storage of time values that are zone-sensitive. This includes the values displayed by functions such as NOW() or CURTIME(), and values stored in and retrieved from TIMESTAMP columns. 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.
Therefore, if you use TIMESTAMP type columns, MySQL will handle timezone conversion for you automatically: just set the appropriate timezone for the session in its time_zone variable.
You are thinking in the right direction.
I would not use the server's timezone. Instead, use Coordinated Universal Time (UTC) time. It is the World Time Standard. This is pretty much the same as Greenwich Mean Time (GMT). Note that UTC does not change with Daylight Savings Time.
TO use in PHP see: http://php.net/manual/en/function.gmdate.php
From here, you can either add hours via: http://www.php.net/manual/en/datetime.add.php
Or set the timezone based on the users preference: http://www.php.net/manual/en/datetime.settimezone.php
The one you use is based on how you get the user's timezone. If you ask them for it (most accurate) you can set the timezone in PHP with the user selecting from a combo box. If you get it from the header with JavaScript using getTimezoneOffset(); then it is best to add hours based on the timezone offset.
I personally set all the times in my DB according to the GMT +0.00 timezone. So I use UTC_TIMESTAMP() (or UTC_DATE(),UTC_TIME() - whichever applies) when I want to add the current time, for example. This is server independent so I'm confident that even if I change my server I will not need to worry about this issue in the future.
Then the options are, if your visitors have a chance to pick their own timezones, you can use the DATE_ADD() and DATE_SUB() functions to format the result before providing results.
Otherwise if you have the chance (this is my favorite solution) you can use Javascript to format that date/time, which you can make it handle easily by something like
function getLocalDate(dt) {
var d = new Date(0);
d.setUTCSeconds(dt);
return d.toLocaleDateString();
// or in some format that you choose
}
which gets the date echoed by PHP using strtotime($row['some_date']);.
I need to store a timestamp for form submissions in a PHP/MySQL form. I would like to be able to query this field with the most flexibility for displaying the data in PHP. What MySQL datatype is best practice, and which PHP function should I use to store/retrieve the data?
I would store this data as TIMESTAMP data type provided by MySQL. This has lots of advantages over other storage options such as DATETIME or INT listed below
It is especially meant to store a particular time instant. No matter which time zone your server is in, and which time zone your mysql client is in, the value of constant CURRENT_TIMESTAMP stored in a TIMESTAMP field will always point to the same instant in the absolute time line.
Internally it uses 4 bytes of space for storage, same as that of an INT data type
It will give you pretty looking, human understandable values in regular queries.
Moreover, these pretty looking values are converted to the timezone of the client connecting to it. This might be good or bad for you. You can always change it to a desired one, including UTC, with set timezone='timezone' if the mysql time zone table is populated or similar to SET time_zone='+5:30'. The latter would work only if there are no daylight saving adjustments in the desired timezone.
Your queries can take the benefit of the fact that MySQL understands this field represents a date. So you could run query like: Get all the registrations happened on month of may for last 3 years.
Use inbuilt functions to change the display format to show unix timestamp or any other valid form.
Use DATETIME or DATE in your database: http://dev.mysql.com/doc/refman/5.6/en/datetime.html
What MySQL datatype is best practice, and which PHP function should I use to store/retrieve the data?
http://ch2.php.net/mysqli
dunno what timestamp you're talking about but for the unix timestamp you'll need INT type field.
I've had some trouble choosing an efficient way to insert a time in mysql database.
I know mysql has its own timestamp system but it is not what I'm looking for.
I currently use date("Y-m-d H:i:s", time()); and it gets the job done.
But I would like to know what the standard is or what the best practices are to insert time that make it easier for retrieval later.
Thanks.
ps. I've searched the site for any dupe posts but I could not find any, if you think this is a dupe feel free to close.
you insert it the way mysql likes to store it, and on retrieval, you format it how you like (DATE_FORMAT()). You don't store it in some other format because that is how you want to display it, as that then breaks all the functionality of the date\time.
The best thing to do is store dates and times in the database in UTC and convert them to the appropriate timezone when displaying them. MySQL even has a special function for "now in UTC": UTC_TIMESTAMP().
When you use PHP's date function to format a Unix timestamp, you are outputting the representation of the date and time in the web server's timezone. That is rarely what you want, and it causes all sorts of problems whenever the web server's timezone changes, such as during Daylight Saving Time or if the web server is physically relocated to a different timezone.
You can store them as a MySQL timestamp and then use UNIX_TIMESTAMP() to convert to unix time for use in PHP, you can also use FROM_UNIXTIME to convert from unix time to a MySQL timestamp when inserting.
SELECT UNIX_TIMESTAMP(`field`) AS `unix_timestamp` FROM `table`;
INSERT INTO `table` SET `field` = FROM_UNIXTIME(unix_timestamp);
If you need your web application to translate between timezones on a per-user basis, why not use TIMESTAMP across the board for all date/time fields? Consider that TIMESTAMP values are stored in UTC and converted to the timezone set for the connection when retrieved.
I have asked this question on IRC, read the MySQL documentation, searched Google extensively, and asked my co-workers, and I have yet to find a compelling reason to not use TIMESTAMP.
Note: I understand TIMESTAMP has a limited range of 1970 - 2038; that is not going to be an issue in my case. Also, I am using PHP with MySQL.
DATETIME is for arbitrary dates and times that you utilize in your data.
TIMESTAMP is for when you want the time to be automatically updated. If you want to know when the row was inserted/updated, use a TIMESTAMP.
Also, keep in mind that a TIMESTAMP is only stored in UTC -- it is converted to the local timezone of the server before it is transmitted back as part of a query.
In your case, you're better off using a DATETIME. If you're worried about UTC, that's fine -- just create your dates in UTC rather than using the local time. Use UTC_TIMESTAMP.
I think your answer is here:
I understand TIMESTAMP has a limited range of 1970 - 2038; that is not going to be an issue in my case.
I would be careful making assumptions about the longevity of projects especially when it comes to database schemas. Databases have a tendency to remain in place and in use long after the applications that used them have gone away.
Randalpho's answer is wrong on many facts!
Timestamps do not need to be automatically updated on creation OR updates.
Also, timestamps are translated to the CLIENT's local time, not the serever's.
Just look at the MySQL docs for datetime.
My server is in Dallas. I'm in New York City.. and both PHP and MySQL have configuration variables for setting the timezone.
How do I get them all to work together? What dates should I store in MySQL? How do I get PHP to handle changing the date based on the user's preference?
Bear in mind: I don't think I'm ever having PHP explicitly set the date, it's always using "NOW()" in queries.. however I foresee the need to do this. How would this be done?
I'm hoping SO's experience can help me out here.
Use Unix Time everywhere. It's using UTC so it's the same for every timezone. Methods for dates usually convert to it and back from it using timezone information they have, so you would have yourself a correct time.
Alternatively you could use Unix Time only to transfer time from one computer to another (like from DB to your server running PHP, or to JavaScript client). There's functions to convert to it and from it in every language. For MySQL it is:
UNIX_TIMESTAMP(date)
FROM_UNIXTIME(unix_timestamp)
That way you could have your time properly formatted on the DB and in logs but still have correct local time everywhere.
I prefer using dates and times in the native form with respect to the environment, that is, Unix timestamps in PHP and DATE/TIME/DATETIME/TIMESTAMP fields in MySQL. I translate both values into another using FROM_UNIXTIME() and UNIX_TIMESTAMP(). I prefer this instead of Unix timestamps, because native dates/times are much easier to read.
Record your dates in GMT (zero offset) and then calculate the offset based on the local timezone (EST is +6, for example, so you'd add 6 hours to the GMT).
Check the Date docs for the date_default_timezone_set() function.
Just remember, when writing to the database, you'll have to change time zones, store the date, then change back. Likewise, when you're retrieving the date, don't forget to add the timezone offset.
The mysql-server stores dates in a timezone independent format (UTC).
But before it stores the date it will be converted using its timezone.
U can change the mysql timezone per connection *1:
mysql_query('SET time_zone = "'.$timezone.'"');
You can also change the timezone per script.
date_default_timezone_set($timezone);
If you set them to the same timezone "2009-01-10 13:30:00" will mean the same thing to both mysql and php.
But keep in mind that the 2 servers have different internal clock values, so if you want to generate timestamps based on current time. Do that in mysql OR php.
*1)
MySQL timezone support may require additional configuration. check the manual