i was wondering if i could use IST for my laravel application, because as my app is for that region default timestamps() values wouldn't work? how to approach that? or is it impossible to do that?
Asked
Active
Viewed 985 times
1
-
sorry i don't have much experience with timezones, but thing is i want that time to be in IST, previously i use to do in code igniter converting it into IST but i don't know how to do the same in laravel – avinashizhere Sep 13 '14 at 13:50
-
1Ok, than take my advice … use date/time information as UTC internally. When the user input is in a different timezone (e..g. implicitely by submitting a date without a timezone), convert it to UTC. If you need to display a date in a local time, convert it from UTC to that time. That will save you loads of trouble. – lxg Sep 13 '14 at 13:52
1 Answers
1
@lxg is correct: Store your times in UTC in your database. Doing this will make it much easier to convert your times to and from different timezones as you need without having to worry about things such as whether or not daylight savings time should be applied or not.
To convert them, simply add accessors to your models to automatically change your data whenever it is accessed:
// app/models/SomeModel.php
public function getCreatedAtAttribute($value)
{
return Carbon::createFromTimestamp(strtotime($value))
->timezone('Asia/Mumbai')
->toDateTimeString()
;
}
This allows you to do a couple of things:
- It will be easy to change the timezone without changing any data that you currently have stored
- You can introduce logic to determine what timezone to convert to instead of hardcoding it (e.g. if you store user specific timezones you will be easily able to display time information in their local timezone).
These of course are only a subset of the benefits that you get when storing time in UTC.

Community
- 1
- 1

Jeff Lambert
- 24,395
- 4
- 69
- 96