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

Lazlothemonkey's avatar

How can I write this eloquent query involving time zones and current datetime?

Hi I can't figure out how to write this scope query using Eloquent in Laravel. This is my latest attempt:

public function scopeUpcoming($query)
    {
        return $query->where('start_date_time', '>', date('Y-m-d H:i:s'));
    }

I have a table called 'Lessons' where the start_date_time is stored as the local timezone. Since Mysql doesn't allow the timezone to be stored in the same date_time column, I have another column in the table calledtime_zone where the relevant time zone is stored. My goal is to compare start_date_time with the current datetime in the timezone of the lesson... , but currently the query is comparing start_date_time with the current datetime in UTC.

Is there anyway to achieve this without having to store start_date_time in UTC? Thanks for any tips.

0 likes
5 replies
Formal's avatar

I'm not sure I understand what you're asking, but maybe this will work for you.

public function scopeUpcoming($query)
{
    return $query->whereDate('start_date_time', '>', now());
}
Lazlothemonkey's avatar

now() returns the current time in UTC, however 'start_date_time' is stored in the local time zone in my case...

Lazlothemonkey's avatar

is there anyway to change 'start_date_time' to UTC in the query?

guybrush_threepwood's avatar

How about using RAW sql?

Something like:

public function scopeUpcoming($query)
{
    return $query->whereRaw('CONVERT_TZ(start_date_time, start_date_time_tz, "UTC") > ?', now());
}


start_date_time_tz would be the column where you're storing the timezone. The function supports numeric timezones such as "'+00:00" and named timezones such as "Europe/Helsinki".

Mind you, the query is untested.

Please or to participate in this conversation.