Get result from query in timezone user rather than timezone DB - php

I have a Mysql DB on time zone UTC and PHP server in Timezone Europe, Bucharest (GMT+03:00).
When query like giving me result in month=8 and year=2018, because different Timezone there's some result in month 7. The result in month 8 in DB Timezone start from 31-07-2018 21:00 to 31-08-2018 21:00 not (start from 01-08-2018 00:00 to 31-08-2018 23:59) is a real result for my client in his time zone.
So is there a way to set Timezone on query and get exactly result meet Timezone of my client?

Related

Correctly converting to timezone from UTC in MySQL database with PHP

I see that a stored date value in phpmyadmin is 2020-03-03. In PHP, I set the default timezone to UTC and the user's timezone to America/New_York:
date_default_timezone_set('UTC');
$userTimeZone = new DateTimeZone('America/New_York');
I retrieve my date from the database, set the timezone on the date, and make it into a string. It now equals a day earlier. (03-02-2020)
$dateNeeded = new DateTime($row['dateNeeded']);
$dateNeeded->setTimeZone($userTimeZone);
$dateNeededStr = $dateNeeded->format('m-d-Y');
What did I do incorrectly here?
It is not incorrect. Your date is stored without the time component, which means when you put it in a DateTime constructor, it will be created as midnight ($dateNeeded = new DateTime($row['dateNeeded']) will have the value of 2020-03-03 00:00:00.
That's in UTC timezone because you defined it so at the start. So when you change the timezone, it will move back (as New York is UTC - 5) and gain the value of 2020-03-02 19:00:00.
Since you're only outputting the date in your format (and not the time), it comes out as a day early.
Dont use date_default_timezone_set('UTC'); because MySQL server will use server timezone not UTC,
In example,
Your server time zone is GMT +2
And you set your server timezone to GMT +0;
Then you read your date on database which stored on GMT + 2 time zone
When you try to convert it to other timezone, e.g. GMT + 5,
It will shift 7 hour not 5 hour

How to calculate average time of day taking into account timezone differences?

I'm using Laravel/PHP/MySQL and storing all dates and times in UTC.
The user can select a timezone (for example Eastern), enter a date and time, and the date and time will be converted to UTC before storing. On retrieval it will be converted to user's selected timezone.
My question is how can you get the average time of day from a series of records taking into account the timezone (preferably in the database query)? The following question addresses average time of day in PHP, but not the timezone issue.
How to calculate average time
Here is what I'm doing:
SEC_TO_TIME( AVG( TIME_TO_SEC( TIME(flights.departed_at) ) ) ) ) AS average_time
This works except for records that span daylight saving/standard time in a region that observes it.
FOR EXAMPLE: You may have a record with the UTC datetime of 2015-08-18 11:00:00 that was entered by a user in EDT at 2015-08-18 07:00:00. Then you have a second record entered with the UTC datetime of 2015-11-10 12:00:00 by a user in EST at 2015-11-10 07:00:00. If you try to calculate the average time of day it should equate to 07:00:00 but instead the result is 07:30:00.
Any ideas how to overcome this? Am I approaching this all wrong?
Thanks in advance.
In short, your code is working correctly. Once the date is in UTC, you'd have to re-calculate whether or not it was entered (1) during daylight savings time, (2) by someone actually observing daylight savings time, and (3) in a place that recognizes daylight savings time.
I can really only think of one way to approach this.
Add some kind of flag when the data is saved to mark the timestamp as DST. You can use this flag to adjust for the hour difference. How you generate this flag is up to you.
If you have all your times stored in Z, and if your MySQL database has the timezones loaded correctly, you can use the zoneinfo timezone name to retrieve your local times. For example,
SELECT CONVERT_TZ('2015-08-01 11:00', 'UTC', 'America/New_York'),
CONVERT_TZ('2015-12-01 12:00', 'UTC', 'America/New_York')
yields
2015-08-01 07:00 2015-12-01 07:00
The point is, the zoneinfo database knows to use daylight saving time or standard time for each datetime value. It doesn't use the current offset, it uses the offset in effect on the date in question.
So, you can retrieve the time of day, in local time, with an expression like this:
TIME(CONVERT_TZ(utc_time_column, 'UTC', 'America/New_York'))
Then, you average those times-of-day in the usual way.

Wrong time zone in mysql

Working with php and mysql.
In test.php file have this simple script:
echo date('Y m d h:i');
echo date_default_timezone_get();
DB::execute('insert into test (date) values (now()) ');
In the browser it displays GMT as a result of server date_default_timezone_get function and date like "2015 07 16 20:06". However in the DB the value is stored with 2 hours added to the date like it was in central europe time (2015 07 16 22:06).
I've checked the mysql variable TIME_ZONE is set to SYSTEM, so I think that the date stored in the DB should be the same as it is displayed from php server? I'm in central europe in the moment, is it possible that mysql knows this somehow and sets the date based on my computer system timezone?
when I run this i mysql
select ##time_zone, now(), utc_timestamp()
i receive:
SYSTEM | 2015-07-16 22:27:22 | 2015-07-16 20:27:22
Also I've see that other mysql variable SYSTEM_TIME_ZONE is set to CEST. maybe that's why i have date stored in central europe timezone? Even if in php i have different time zone and mysql TIME_ZONE is set to system?
Date field in the table is datetime format

Converting from GMT to UTC

In a table all the records are stored in GMT time. But through my application i want to display only those records which falls into timezone UTC. i.e., in a web page i want to display only records that comes under UTC time zone.
Converting from GMT to UTC. Or Query the database to get all the records of UTC timezone.
I really appreciate an early reply.
I am using oracle database and application in PHP.
From Greenwich Mean Time on Wikipedia:
It [GMT] is arguably the same as Coordinated Universal Time (UTC).
Regarding converting, you can add/remove intervals, but in so far as I'm aware, Oracle supports timestamps with/without time zones:
select now() at time zone 'UTC' as utc,
now() at time zone 'EST' as est,
now() at time zone 'Europe/London' as london;
The last example, if it works, would allow you to not worry about daylight savings and so forth.
I'll assume that your times are stored as DATE values, that all the values are stored as UTC times, and that the timezone you're interested in is constant. To convert from UTC to a given timezone you add the timezone's offset. In this case, since the timezone of interest has a negative offset you need to add in the same negative number. Thus, the following might be useful:
SELECT DATE_FIELD + INTERVAL '-5' HOUR
FROM SOME_TABLE
WHERE <whatever>
FWIW, there are some places where the conversion to local time uses a non-whole-hour offset - for example, Adelaide, Australia uses a +9.5 hour offset from GMT, and Kathmandu, Nepal uses +5.75 hours.
Share and enjoy.
EDIT: Given the data as you've described it, your best bet is probably to simply add in the session time zone, as follows:
SELECT your_gmt_timestamp_field AT TIME ZONE SESSIONTIMEZONE
FROM your_table
Give this a try and see if it helps.

Convert UTC timestamp from database into Users Local time

Question: How do I Display a UTC timestamp from a database table in a users local time? Is there any way to do this with php?
Extra Context \/
I have an application that saves the date records are added to the database.I am saving the records in the database using mysql UTC_TIMESTAMP(). I then want to display the record to the user in their local time in my Smarty template.
{$dateAdded|smarty_modifier:"%D %R %Z"}
I have tried smarty, date_format as the modifier but I just get the UTC time and the persons timezone, which of course is correct but not the result that I want.
02/11/11 20:32 Pacific Standard Time
I need the time so be set to the user's local time
02/11/11 12:32 Pacific Standard Time
Is it correct to store the date of the transaction in UTC, and how do I display that UTC to the user in their local time? Is there a way to pull the date out of the database so that it is set to the users current local time?
You can use date_default_timezone_set() prior invoking the Smarty template. That defines directly how the UTC timestamps are adapted to the expected output timezone.

Categories