1

I have an application where dates are currently stored in the database as UTC. However the audience is primarily UK based so I need to convert all dates back to 'Europe/London' time when displaying or searching using eloquent.

I know I can change the default timezone in app.config which would solve the problem:

'timezone' => 'Europe/London'

However I want to keep it as UTC if eventually the app caters to a more global audience.

Now I know I can change the dates using carbon as follows:

$model->start_at->tz('Europe/London')

Without having to append tz('Europe/London') to every date, is there a way to default them to 'Europe/London' whilst keeping the app.config timezone setting to UTC?

Also note some dates are user entered and so for now using the 'Europe/London' timezone would suffice but eventually, for a global audience this would need to be presented back to the user in their local timezone and for working out eloquent queries where dates need to be compared. So do I convert them to UTC when saving to the database and convert back to local timezone when retrieving them?

adam78
  • 9,668
  • 24
  • 96
  • 207
  • Is this to be applied to all models in your application, or just one model? – thisiskelvin Oct 17 '19 at 09:58
  • @thisiskelvin all models – adam78 Oct 17 '19 at 12:43
  • @thisiskelvin one way is to perhaps add getters and setters to mutate the dates on each model using carbon – adam78 Oct 17 '19 at 12:44
  • This wouldn't solve the problem entirely. For queries, you would still need to convert the dates to the timezone of your choosing before returning results. This is because it would not be recommended to query the returned collection after the original DB query (May produce a performance issue). – thisiskelvin Oct 17 '19 at 12:58
  • So yes, getters and settings would be good for when working with returned results, but not so much for actual queries (e.g. get records between this date and that date). – thisiskelvin Oct 17 '19 at 12:59
  • @thisiskelvin so how would i do that for queries? – adam78 Oct 17 '19 at 14:18
  • See this answer: https://stackoverflow.com/questions/2187593/can-mysql-convert-a-stored-utc-time-to-local-timezone you would need to convert dates within your queries – thisiskelvin Oct 17 '19 at 14:26

0 Answers0