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

colinlongworth's avatar

Converting UTC to an arbitrary timezone for reporting

I have a system which recieves a webhook from an ecommerce system. The Laravel system is configured to use UTC. The database is PostgreSQL. The webhook is used to create an Order model.

To align the creation date, I use the webhook date e.g.

$order->created_at = $webhook->created_at;
$order->save();

where the webhook would have the format:

"created_at": "2018-01-26T22:23:20.808Z"

And the equivilent database column would be:

2018-01-26 22:20:20

The question is how to run a report for an arbitary timezone. For example, we want to export all orders from yesterday in EST, but this code will, of course, use UTC:

$base_date = new Carbon('yesterday');
$start_date = $base_date->startOfDay()->toDateTimeString()
$end_date = $base_date->endOfDay()->toDateTimeString();

$orders = Order::where('completed_at', '>=', start_date)->where('completed_at', '<=', $end_date)->count()

Is the only option to do something like this?

$end_date_est = CarbonImmutable::parse($end_date)->setTimezone('America/New_York');
0 likes
1 reply
LaryAI's avatar
Level 58

To convert the UTC time to an arbitrary timezone, you can use the setTimezone() method provided by the Carbon library. Here's an example of how you can modify the code to convert the UTC time to EST:

$base_date = Carbon::yesterday('UTC')->setTimezone('America/New_York');
$start_date = $base_date->startOfDay()->toDateTimeString();
$end_date = $base_date->endOfDay()->toDateTimeString();

$orders = Order::where('completed_at', '>=', $start_date)->where('completed_at', '<=', $end_date)->count();

In this example, we first create a Carbon instance for yesterday in the UTC timezone. We then use the setTimezone() method to convert it to the EST timezone. Finally, we use the converted date to filter the orders.

Please or to participate in this conversation.