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

Stank0V01's avatar

Current time-zone for carbon on many records.

Hello guys. So i'm trying to set correct time-zone on Carbon::today() for getting all of the results created for this day. Let's explain a little bit more.

I have a command that archive the Notes when created_at column is today, so I get all Notes that are created today and check them in foreach based on Building model timezone

Database structure:

Buildings:
	-id
	-name
	-timezone

Notes:
	-id
	-name
	-created_at
	-updated_at
	-building_id (reference id on buildings)

My code:

$notes = Note::where('archived', null)
		->whereDate('created_at',Carbon::today())
		->get();


foreach($notes as $note) {
	// Set the current timezone 
	date_default_timezone_set($note->building->timezone)

	$note->update([
		'archived' =>1,
		'archived_at'=>Carbon::now()
	]) // update archive status etc...
}

I want Carbon::today() to be with the current timezone of the record building.

I've tried with that query too:

select
    *
from
    `notes`
    inner join `buildings` on `notes`.`building_id` = `buildings`.`id`
where
    date(`notes`.`created_at`) = CONVERT_TZ('NOW()', 'UTC', `building`.`timezone`);

Is there any way to do that? Thanks. :)

0 likes
1 reply
Edison_Relable's avatar

Hmm.. It looks that the query you wrote it is wrong. The query will get only first record of the buildings and replace everything in the second table with the first timezone.

You say that you have a relationship between Buildings and Notes. So you can do something like that?:


$buildings = Building::all(); // Get all of your buildings.

foreach($buildings as $building) {
	$note = Note::whereDate('created_at', Carbon::today($building->timezone))
}

Then you can continue with your other logic.

1 like

Please or to participate in this conversation.