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

Inquisitive's avatar

Laravel timezone issue for search data

I am trying to get results from the start date to the end date as per user timezone and when a user enters the start and end date, first I convert it to UTC as follows:

$start_date = "2022-02-21"
$end_date = "2022-02-26"

Let's consider user is in GMT +10, and it is actually the next day if we compared it with UTC. Now, on start, I convert it to UTC as:

        $start_date = Carbon::parse($start_date, AppHelper::userTimezone())
            ->setTimezone(config('app.timezone'))
            ->startOfDay();
        $end_date = Carbon::parse($end_date, AppHelper::userTimezone())
            ->setTimezone(config('app.timezone'))
            ->startOfDay();

This will update the start and end dates to

$start_date = "2022-02-20"
$end_date = "2022-02-25"

Now, I query data in between these dates and got the result correctly. But, user had queried between range, 21 to 26 (according to his timezone). Now, when I try to convert it to the user timezone, with something like:

$newDate = Carbon::createFromFormat('Y-m-d H:i:s', $date, 'UTC')
                        ->setTimezone(self::userTimezone());

It is returning as : 2022-02-20 10:00:00, which if I format to Y-m-d, it will show results from 20-25, which is wrong since user-requested between 21-26.

0 likes
8 replies
sr57's avatar

@inquisitive

Let's consider user is in GMT +10, and it is actually the next day

2 start hypothesis

->setTimezone(self::userTimezone());

Only one used at the end.

Depending of your initial start date, when it's the next day, you have to add 1 day.

Other solution code your query with tz.

Inquisitive's avatar

If i used only one at the end i dont get it could you please elaborate it little bit mor

sr57's avatar

->setTimezone(self::userTimezone());

How do you define that "user are the next day" with this code?

Inquisitive's avatar

Its because user timezone is stored in session.. and this is what will be returned by this method..

sr57's avatar

@inquisitive

Seems you don't understand your pb and my question, let's see if tomorrow it'll be clearer.

Snapey's avatar

use start of day before changing the timezone.

Inquisitive's avatar

@snapey you mean here ?

        $start_date = Carbon::parse($start_date, AppHelper::userTimezone())
            ->setTimezone(config('app.timezone'))
            ->startOfDay();

I didn't set it, because the start_date would always be on 00:00:00; Same case at here

$newDate = Carbon::createFromFormat('Y-m-d H:i:s', $date, 'UTC')
                        ->setTimezone(self::userTimezone());

$date would always be on 00:00:00, this is what stored on release_date datetime column

Snapey's avatar

@Inquisitive What do you want? When you change timezone then the time will no longer be start of day?

If only dealing with dates then I don't see how you can correctly resolve this because you don't know what records belong to which day in the user's timezone.

You should always store the time in these circumstances.

If the user, in their +10 timezone wants records for 20th to 26th then they want

$start_date = "2022-02-19 14:00:00"  //UTC
$end_date = "2022-02-25 14:00:00"  //UTC

not just 19th to 25th

Please or to participate in this conversation.