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.