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.
Related
I have this date from my local timezone "2019-07-27T02:00:00"
I'm trying to save it to the database in a UTC timezone, so I can deal with it later and convert to to other timezone from UTC,
But when I do:
$date = new \DateTime("2019-07-27T02:00:00");
I got a:
DateTime #1564192800 {#519
date: 2019-07-27 02:00:00.0 UTC (+00:00)
}
Whatever I set here,
new \DateTime("2019-07-26T06:00:00")
I still get a UTC timezone, so this example
$date = new \DateTime("2019-07-26T06:00:00");
Will get me a result of:
DateTime #1564120800 {#519
date: 2019-07-26 06:00:00.0 UTC (+00:00)
}
It's like the timezone of the date (2019-07-26T06:00:00) is already in UTC? But it's not (?).
So, converting it to UTC with
$date->setTimezone(new \DateTimeZone('UTC'))
has no affect at all.
PHP probably thinks you're already in UTC, and accepts any DateTime object that doesn't have timezone explicitly listed as UTC as well.
You can check what PHP thinks is your timezone by looking in your php.ini file, or by using the command echodate_default_timezone_get(). Similarly, you can edit your timezone globally by editing your php.ini file or using date_default_timezone_set().
This is a list of timezones PHP can use.
As a side note: though it might seem like a good idea to keep your global timezone set to UTC and just use a DateTime/DateTimeZone to set local variables to the correct offset, don't do it. It'll affect the timestamps of your logs, and can lead to some very painful gotchas.
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 am using timezone to get exact time of a user according to his timezone. I have a drop down list to select timezone for users and showing their current time as they selected their timezone. The code i am using is:
$timezone = 'America/New_York';
date_default_timezone_set($timezone);
echo date('H:i:s A');
It is working fine. I want to get locale of that user using timezone in the same way I am getting current time for that timezone.
How can I achieve the current locale using timezone? Does PHP have any kind of solution to get current locale using timezone?
Locale and time zone are orthogonal. You cannot determine one from the other.
America/New_York means that the user's local time is aligned to New York City, which happens to be called "Eastern Time" in the United States, which is 5 hours behind UTC during standard time and 4 hours behind UTC when daylight saving time is in effect.
en-US (or en_US) means that the user speaks English, with cultural dialects (word choice, numbers, dates, etc.) of the United States. For example, en-US uses MM/DD/YYYY date format, and en-GB uses DD/MM/YYYY date format, but we both call our first month "January", while es-MX calls their first month "enero".
I could very well be an English speaking American visiting Japan, thus my time zone would be Asia/Tokyo even though my local would still be en-US.
Aside: A hyphen (-) is the correct character to split language and country codes in a locale identifier. Though some implementations have substituted an underscore (_), this is not correct by the IETF language tag specification.
How I can create a new date with different timezone? I have tested many ways, but doesn't work, example
$date = new DateTimeZone('UTC+5');
My goal is to create the $date, then convert it to "UTC-5" format.
UTC+5 is not a timezone, it's an offset. A location on earth, say, New York, America, has a local time which is x hours different from the UTC standard time. That's the offset, like UTC-5. However, this offset changes throughout the year based on daylight saving time. In winter the offset may be -5, but in summer -6. A timezone is something which describes this clearly, it's a ruleset for what offset the local time is to UTC and when that offset changes. A timezone is specific to a location and is called, for example, America/New_York.
Therefore, working with "UTC-5" offsets is pretty meaningless, and what you really want to do is use a timezone for the location you're working with.
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().