Laravel WhereDate Filter in Auth User Time zone - laravel

My default Laravel application timezone is America/Los_Angeles (pst), I'm storing all the timestamps like created_at with this timezone in database.
In the user profile, we are providing options to select a timezone. While showing the list of data for example in trip listing I'm converting & showing created at as per user selected time zone ( $date->setTimezone($user->timezone);)
For example, if the trip Id 197 has created_at 2020-06-11 23:00:00 stored in db (as per default application timezone i.e. pst) while in the listing I'm showing 2020-06-12 02:00:00 (est timezone as per user profile 3 hrs ahead).
Now everything works fine until I had to add date range (start & end date) filter in the listing. The problem is if I'm selecting start date 2020-10-12 in the filter, in result it is not getting 197 trip id because in the database it is stored as 2020-06-11 23:00:00., this 197 id record should be there in listing after filter because as per auth user timezone the trip is added on 2020-06-12. My DB query is $trips->whereDate('created_at', '>=' ,$request->start_date);. I have the only date and not time in request for filter trips I need to somehow pass timezone in this query or is there any better solution for this. The date filter should work as per user selected timezone

if anyone faced a similar problem following is the answer I found Generally for date range filters you’ll want to make sure you’re setting the start dates time to 00:00 and the end dates time to 23:59
if($request->filled('start_date'))
{
// $request->start_date;
$date = Carbon::parse($request->start_date, auth()->user()->timezone)
->startOfDay()
->setTimezone(config('app.timezone'));
$brokers->where('created_at', '>=' ,$date);
}
if($request->filled('end_date'))
{
$end_date = Carbon::parse($request->end_date, auth()->user()->timezone)
->endOfDay()
->setTimezone(config('app.timezone'));
$brokers->where('created_at', '<=' ,$end_date);
}

When you can have different timezones for 1 column in a table, you need dateTimeTz to store both datetime + timezone for each row.
With this, the whereDate will use the timezone stored, then you can reconvert to any other timezone on need without loss.

Related

Filter a field with timestamp (datetime) to get rows that does not contain today's date in Django

I have a Django model with a timestamp field. I want to filter and get the number of rows that does not contain today's date. The timestamp field contains the date, time, and time zone.
I understand that to get rows with today's date, we do the following:
tablename.objects.filter(timestamp__date=date.today()).count()
Thank you.
.exclude returns the inverse of .filter, so it would be
tablename.objects.exclude(timestamp__date=date.today()).count()
Django docs

Format dates in response field in elastic search

My server on which the application runs (uses ES) and ES are in central american time and I am accessing it from my server which is in IST.
I want some charts and data for today(Say Jan 3) so I send from date as today 12:00 AM to to date as current date in UTC to date range query. I get accurate number of records but I want the date to be shown as todays date alone(Jan 3), It gets divided between today(Jan 3) and yesterday(Jan 2) on client side access - ideally it should only todays date on client side for all records. Kibana renders correctly. So obviously there is a way and I am missing someting
I am using date range query - passing UTC for both to and from date and timezone offset as well.
Also, I am querying on a field which is of default type Date.I have not specified any custom date formats
Can somebody pleas help me with this?

Control UTC time in server side Dynamicscrm

I saved a record in 17:16:15, I run a job which gets the ModifiedOn field of my record and I got- 15:16:15, my GMT is +2, I want to know how to fix that gap that my result will turn out like it should be - 17:16:15. I can't select it from DB I need a solution in server side (c# I mean) what can U do in that case?
DateTimes are always saved in UTC in the database. *
You need to dynamically convert from UTC into your local time zone. In C#, you can do this with the .ToLocalTime() method as long as your code is running in the correct time zone. You can also find your local time in the FormattedValues collection of the response, which uses your Dynamics timezone user settings . But the raw datetime value in the database will always be in UTC.
* The only exception to this is if the DateTime field is set to “TimeZone Independent” in the attribute type settings. But be careful: once you set this option you can’t change it for that field again.

What's a good way to implement a date filter in a list where the items have different timezones?

We have an app that lists sport events. An event is associated to 2 teams (home team and away team). In the event list, we are showing the date of the event in the home team's timezone. We are storing dates in the database in UTC. We need to add a date filter in the event list page, but the problem is, since we are storing the date in our db in UTC and the dates in the event list is being shown in the home team's timezone, the date filter is showing unexpected results. the date is saved in our db as 2016-02-18 03:30:00+00 (UTC). When shown to the user in the event list it is 02/17/2016 7:30pm because the home team is in Pennsylvania and the timezone is America/New_York.
now when the date filter is set to 02/17/2016 by the user, we are querying the the db to fetch all events where the date is 02/17/2016. It will cause unexpected results since it will not match the event because in our db the date is 02/18/2016.
One solution I can think of is show 2 date & time columns in the event list.
Date (UTC) | Time (UTC) | Date (Home team's timezone) | Time (Home team's timezone)
And inform the user that when using the date filter it will filter the events using date (utc)
But we're currently looking for a better solution. Any help / suggestion would be greatly appreciated :)
EDIT:
event 1:
- date: 01/12/2016
- time: 9:00am
- home team's timezone: America/Chicago
event 2:
- date: 01/12/2016
- time: 9:00am
- home team's timezone: America/Denver
event 3:
- date: 01/12/2016
- time: 9:00am
- home team's timezone: Asia/Singapore
if the user selects 01/12/2016 in the date filter. It should still show the 3 events above.
Add a new field in the database which stores the dates in its local time and use that field to filter the results.
so when you filter say, 2016-04-01, it will actually be filtering things across different timezones i.e. events that are not at the same Epoch time
so show me all events for e.g. Thursday, regardless of where the teams are (meaning that one team’s thursday is another team’s friday)
it will filter results that are localised to wherever the date was stored from.
Another solution is to use on the fly conversion query.
Example:
where convert_to_local_without_timezone(event_datetime, event_tz) = date_filter
It can be a user defined function - http://www.postgresql.org/docs/8.0/static/xfunc.html if there's no existing function that can achieve this.
Credits to:
Aries, Ryan Johnson and Nikki Erwin Ramirez

How can I change the timezone of the outputted date in Laravel 4?

I have a database with an entity table and a users table. The entity table has fields for created_at and updated_at. The users table has a timezone field with a default value of America/Toronto.
In my view, I can output the date in the format I want with the following code:
$entity->created_at->format('M j, Y \\a\\t g:i A')
Now, how can I output the date using the timezone field in the users table?
Something like ...
$entity->created_at->format('M j, Y \\a\\t g:i A', Auth::user()->timezone);
I am looking for a simple way to do this with Carbon (since I am using Eloquent in Laravel, which will convert the above date columns to instances of Carbon).
Note: I only want the timezone to be changed when presenting the date - I still want all dates to be stored in the default UTC timezone in the database.
Also, where can I find a valid list of time zones that can be used with Carbon? I would like to store these in my database so users can change their default time zone.
You need to use the Carbon function copy() to keep the timezone unchanged on your object. Then you can just set the timezone to whatever you want and 'output' the result.
$entity->created_at->copy()->tz(Auth::user()->timezone)->format('M j, Y \\a\\t g:i A');
As for timezones - it is just PHP timezones.
$tzlist = DateTimeZone::listIdentifiers(DateTimeZone::ALL);

Resources