How can I change the timezone of the outputted date in Laravel 4? - 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);

Related

Recyclerview sort by current date

In the recyclerview, I have data in the format dd/MM/yyyy with a period titled date.I want this data to list only the dates that are less than the current date.I am using sqlite as database.
you should use ORDER BY in your sql request. And should format date in milliseconds before pass it into database. Then, when you called data in recycler view use SimpleDateFormat for formatting date into dd/mm/yyyy. For current date use Calendar.getInstance.getDateInMillis

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

Laravel WhereDate Filter in Auth User Time zone

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.

Laravel - What's the difference between timestamp() and timestampTz()?

What's the difference between timestamps() and timestampsTz() methods in the Laravel's Schema Builder? I tried searching google but couldn't find any help.
Basicly timestampsTz() stands for Timestamp with Timezone while timestamps() is like Timestamp without timezone
TimestampsTz() is a representation for a specific point in time. The adjustment to this time is made by the timezone that is related to your system.
The normal timestamps() is more a representation like normal clock.
Have a look at this example:
If you are using the timestamps() function you will also store the timezone that was chosen by your environment. If you are using the timestampsTz() you will not safe the timezone.
I guess it would be good practice to use timestamp without timezone when all your data is for sure in the same zone, or you got another layer over there which handles the time conversion.
Update:
In the Database the normal timestamps() will look like
2004-10-19 10:23:54
while the timestampsTz() looks like
2004-10-19 10:23:54+02
Update 2:
These functions are in Laravel available for every type of Database. But this only differs for PostgreSQL. Have a look at the docs: PostgreSQL Timestamp. In the other databases the timezone information is included in the timestamp automatically.
In all other databases this will have the same output.
timestampsTz() = Adds nullable created_at and updated_at TIMESTAMP (with timezone) equivalent columns.
timestamps() = Adds nullable created_at and updated_at TIMESTAMP equivalent columns.
The difference is that timestampsTz() adds a timezone.

Can't remove hours and minutes from datatable

I'm trying to get release date field with y-m-d format.. Actually time format (for y-m-d) seems fine but also it gives h:m:s too, I changed time format at server side and removed h:m:s but datatable still shows them
What I get
2012-04-11 00:00:00
What I want
2012-04-11
Released_at field (Metronic theme - json datatable)
{
field: "released_at",
title: "Release Date",
type: "date",
format: "YYYY/MM/DD"
}
time format (I'm using laravel framework)
protected $dateFormat = "Y-m-d";
How do I fix this ?
Datatable accept column type same as mysql type.
You are trying to convert mysql dateTime column to Date in datatable, which is not possible from datatable.
In your code you have declared
format: "YYYY/MM/DD" // but actual type is dateTime as per mysql.So it will append time next to it.
If you are storing only date in mysql then you can change column type to Date, and your problem gets solved.
And if you want to convert string to date in laravel you can follow this post.
Your database column is of type dateTime which, by definition, includes both date and time information. If you want to remove the time at a database level then use the date type instead
$table->date("released_at")->nullable();
If instead of removing the time from the database, you just want to ignore it for certain parts of your application, you can leverage the fact than in Laravel all dates coming from your models are Carbon\Carbon instances, so you could do
$model->releasedAt->format('y-m-d'); // Returns '18-09-11'

Resources