Welcome Guest, Not a member yet? Register   Sign In
Timezone handling suggestions with CI4

I'm curious what others do to handle timezone issues with CI4. Conceptually, I am currently of the opinion that dates should be stored as UTC. Front end should display whatever timezone is appropriate for the user. For the sake of this discussion, let's assume 'America/Chicago' is the timezone of the user.

I am taking advantage of CI4 Entities, which is super helpful for many of the tasks I need. CI4 has a config setting in the App config class called $appTimezone that is used to display dates. Conceptually, should I treat the CI4 config value $appTimezone as synonymous with "front end timezone", or should $appTimezone be the same as my storage timezone? So, in this scenario, should $appTimezone be 'America/Chicago' or 'UTC'?

This decision will dictate where in the process of handling user input that I need to make timezone conversions. I sort of suspect the intention of $appTimezone is to be the "front end timezone." If that is correct, then that means I will have to make some clever coding to extend the default Model methods for saving Entities. There seems to be no obvious way to tell CI4, "I want to store all dates in UTC." On the other hand, if I make the $appTimezone 'UTC', then I will have to remember to intercept all $_POST dates and remember to convert them from 'America/Chicago' to 'UTC' (unless there is some clever way to handle this with Entities). Anyway, I'm hoping someone with more experience has a good suggestion for how best to handle this issue in CI4.

Thank you so much for any suggestions.

I too am on the crossroad with dates for a new project i am about to start - and dates will be crucial here, as users will be spread across 3 continents at least.

After reading a bit - it is indeed the most common scenario to keep dates in UTC in the database (no surprises there). However i think that the the $appTimezone setting is just the default app time to be displayed (no conversions are made there) - its just used in system/CodeIgniter.php like date_default_timezone_set($this->config->appTimezone ?? 'UTC'); so that when you call date() it returns based on your current timezone setting.

That said i think that requests containing dates to be written in the database should be intercepted and modified. I am looking for a convenient way to do that (as dates will be used massively in my app).

I am also open to any suggestions

I'm interested in this user case too, as I'm a noobie in PHP and CI4 and I'm thinking to start a project in CI4 wich is based on user's timezone for date and time calculations... And I'll need to do proper date and time formatting, based on user's country/language/timezone...

Any suggestions will be appreciated!

If you call date_default_timezone_set() in your BaseController it will override the CI's $appTimezone value. The model will use the timezone that you specified and will save recoreds in DB with your timezone.
If you store dates as timestamp you can easly format them with this CI method:

Theme © iAndrew 2016 - Forum software by © MyBB