Hi i am using PHP to store the timezone.The timezone coming from dropdown and when I try to store it if its (GMT+5.30) or (GMT+anything) the plus symbol doesnt stored on database.It **stored as (GMT 5.30).**How can I store it correctly?
Store all your times in UTC, that way you can easily convert them to any timezone that you require.
As for the timezone - I would store this in a seperate column either with the offset from UTC or even better store the locale of the timezone e.g. "America/New_York" that way you can accomodate for daylight saving times also.
$url_safe = urlencode($string);
Related
I have a mysql database in this format
And I am trying to fetch the values through a php document and convert them into json on the timezone of the user (or maybe just GMT-6 would suffice) but the json outputs from the php document are as follows:
[{"timestamp":"2018-06-13 19:52:05","temperature":"79.83","humidity":"41.89","pressure":"99.35"},{"timestamp":"2018-06...
Still in UTC time, I have tried adding
date_default_timezone_set('America/Los_Angeles');
To the php document, but the time never changes, how would I solve this?
Use convert_tz() on the timestamps in your query to convert them from one timezone to another, e.g.:
... convert_tz(`timestamp`, 'Etc/UTC', 'America/Los_Angeles') ...
Make sure to follow the procedure "Populating the Time Zone Tables" as described in "5.1.12 MySQL Server Time Zone Support" to have the time zones available or to check which are available in your system.
I'm currently using this to convert a timestamp to a user's defined timezone. My problem is that DateTimeZone() requires a timezone like Europe/Vienna or America/Chicago:
$date = new DateTime("#".$timestamp);
$date->setTimezone(new DateTimeZone("America/Chicago"));
I already looked into supported timezones on http://us3.php.net/manual/en/timezones.php but there are so many of them and I don't want users to browse the whole list.
Is there a simple way converting e.g. GMT+1:00, GMT-4:30 or GMT+5:45 to a correct value for DateTimeZone().
Or is it better to use an array list like I found here: Convert selected time and timezone to a set timezone
Is it better to use UTC or GMT in general for the user to pick?
Thanks!
You should probably obtain the user's real timezone (selected from the complete list). Using only a static offset from UTC you will not be able to follow the correct daylight savings time rules for the user's location.
PHP's DateTimeZone doesn't appear to accept POSIX timezone strings so it looks like you're stuck with predefined timezones.
Look in /usr/share/zoneinfo/Etc. There are a bunch of timezones predefined there for UTC plus and minus an integer. The only gotcha is that the sense of + and - is reversed from the normal convention. So just use, for example, "Etc/GMT-9" for a generic timezone with offset +0900.
This doesn't handle all of the possible timezones, like Nepal's weird +0545 but it looks like it's the best option that's easily accessible.
I don't think there's another way to pass a timezone. I think the time zones used in PHP have to be location-based to determine the DST settings along with the offset.
However, you can still present the options as GMT+1:00 etc to your users. And you can narrow down the list to only the much-used options (UTC, GMT, PST, etc).
I have a dropdown box on a webpage that takes a timezone identifier as the value (however I can change it to save the GMT offset if that is a better choice). Currently a user would be saving something like "America/Denver" as his / her time zone.
I need to convert a unix timestamp into the user's local time for display in another portion of the page. What is the best way to accomplish this?
Would it be better to store the GMT offset in the database and use that for a time calculation? Can someone please point me in the right direction here?
Thanks!
The easiest approach would be to use DateTime with:
$t = new DateTime();
$t->setTimestamp( $time=time() );
$t->setTimeZone(new DateTimeZone("America/Denver"));
print $t->format(DateTime::RFC850);
The timezone can be changed at will to cause the format function to recalculate the local time.
Suppose now I've got the datetime to show like this:
2010-05-29 15:32:35
The the corresponding time zone can be get by date_default_timezone_get,
how do I output the result in the same time zone as user's browser so that users don't get confused?
There is no reliable way to read the user's locale timezone settings from PHP or JavaScript.
In JavaScript you can read the offset between UTC and the time in the user's current timezone, but that doesn't give you a timezone name, so you're left either leaving the timezone identifier off (making the times completely ambiguous) or including it is an explicit offset like UTC+01:00, which is ugly, and still doesn't take care of changing timezones over different DST periods.
As bobah says, you can use toLocaleString() on a JavaScript Date to output it in the client's real desktop timezone. However, this way you get no control at all over the date formatting. For example on my machine Chrome outputs the unwieldy:
Sat May 29 2010 15:03:46 GMT+0200 (W. Europe Daylight Time)
Whereas Opera just coughs up:
29/05/2010 15:03:46
Which, as it doesn't state the timezone at all, is uselessly ambiguous. IE is similarly terse, and Safari states no timezone either. Firefox does for me on Linux, but not on Windows. Argh.
So if you need reliability the only way to handle timezones is to let the user manually choose one in their site settings, then convert from UTC to that timezone when you're producing a page for them. You can make a guess in JavaScript as to which the most likely of some common timezones it might be (see this question for strategies), but you can't guarantee you'll be right.
You can pass UTC timestamps to the page and convert them with JavaScript. I used this trick once and was happy with the result. There is a constructor of JavaScript Date taking UTC timestamp. For UTC timestamp generation from PHP one may use gmmktime().
I have a list of dates stored in MySQL using PHP.
These were stored using the following code:
date_default_timezone_set('UTC');
$strDate = date("Y-m-d H:i:s",time());
I can only test this from my timezone, which is also UTC!
If a web visitor from eg Eastern Time USA views the page, will the date be converted to UTC correctly?
Presuming that I am storing the UTC dates correctly, what PHP function will display the UTC time, converted to the user's own timezone??
Firstly you're right to store the UTC; however remember that the date you have in PHP will be the server date - not the client date.
So to continue, read up on how to extract timezone based dates. Then consider how to extract the timezone from the browser - which you will need if you do the local timezone output in PHP rather than on the client.