lundi 4 juin 2018

Laravel orderBy datetime with timezone

I'm ordering a list of events by datetime which works fine, however a problem occurs when some items have a timezone applied to them - they don't order correctly. E.g.

Event 1 - Ends 20th June 2018 at 15:00:00 (Europe/London)

Event 2 - Ends 20th June 2018 at 13:00:00 (US/Eastern)

When these are ordered by date ascending 'Event 2' shows BEFORE 'Event 1' because timezone is ignored (when in real time it's 2 hours later)

I have tried using a mutator in Laravel to convert both datetimes and timezones to timestamps:

 public function getDatetimeTzSecondsAttribute()
{
    $newDate = new Carbon($this->attributes['date_finish'], $this->attributes['timezone']);

    return $newDate->toTimeString();
}

But understand this won't work as mutators are processed after results have been fetched. I am using pagination for results - whats the best way to order this data via the query builder/eloquent if possible?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire