Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kuns25's avatar

Where Date Filter in Auth User Time zone

Hi, my default laravel application timezone is America/Los_Angeles (pst) , I'm storing all the timestamps like created at with this timezone.

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 showing created at as per user selected time zone ( $date->setTimezone($user->timezone);)

For example, if the trip Id 197 has created_at 2020-10-06 23:00:00 stored in db (as per default application timezone i.e pst) while in the listing I'm showing 2020-11-06 02:00:00 (est timezone as per user profile).

Now everything works fine until I had to add end date filter in the listing. The problem is if I'm selecting 2020-11-06 in the date filter, in result it is still getting 197 trip id because in the database it is stored as 2020-10-06 23:00:00. My db quert is $brokers->whereDate('created_at', '>=' ,$request->end_date); . I have 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.

0 likes
7 replies
Entropi's avatar

You should be able to do something to the effect of:

// Create an instance of the submitted filter date in the user's timezone.
$date = Carbon::parse($request->end_date)->setTimezone($user->timezone);

// Convert said date to the application timezone and query using it.
$brokers->whereDate('created_at', '>=', $date->setTimezone(config('app.timezone'))->format('Y-m-d'));
kuns25's avatar

Hey i tried this, the thing is in request I'm only getting date and not time, so date in any timezone will be same. But my created_at contains timestamps

Entropi's avatar
Entropi
Best Answer
Level 8

Slight tweaks, key part being to add the start of day time before converting into the application timezone.

$date = Carbon::parse($request->end_date, $user->timezone)
	->startOfDay()
	->setTimezone(config('app.timezone'));

$brokers->where('created_at', '>=', $date);
kuns25's avatar

thanks man. I really appreciate all the help , following is the code


if($request->filled('start_date'))
		{   
			 // $request->start_date;
			$date = Carbon::parse($request->start_date, auth()->user()->timezone)
			->startOfDay()
			->setTimezone(config('app.timezone'));

			$brokers->whereDate('created_at', '>=' ,$date);
		}   

		if($request->filled('end_date'))
		{   
			$end_date = Carbon::parse($request->end_date, auth()->user()->timezone)
			->endOfDay()
			->setTimezone(config('app.timezone'));
			$brokers->whereDate('created_at', '<=' ,$end_date);
		} 


grimaldo15263's avatar

Hello, laravel-timezone development by creating an easy way to set a timezone for a user in your application and then show date/times to Illuminate\Auth\Events\Login event and will then automatically set a timezone on change the format property inside the configuration file config/timezone.

https://www.mygiftcardsite.win/

Please or to participate in this conversation.