Users can set their timezone and we store it in the database. Then we can dynamically change the system timezone on each page load in Middleware or in our AppServiceProvider.
Example: https://stackoverflow.com/a/47938555/5156146
The problem with this is that it messes up our logs. Any logs will be in the user’s local timezone instead of UTC.
How can we set the timezone dynamically for each user while still keeping the timezone correct in our log files?
Currently we use date()
and now()
in many places throughout my code. I want these to all be set to the user’s timezone but still have any logs stored in UTC.
2
Answers
in Carbon, we can use localization, for example:
this will generate timestamps based on Jakarta, Indonesia.
You can store your user’s timezone in the database and access it on user’s controller for the localization, and use your desired timezone for your log.
here’s a list of supported timezones : PHP Supported Time Zone
Changing PHP’s default timezone will alter the timezone used for the logs. To avoid that, don’t change the default timezone. You can even block the default timezone functions from being used though this might cause errors if you have any code/libraries that uses them. There are various methods to get formatted date & time without changing PHP’s default timezone, using the
date()
andnow()
functions are not among them.Using pure PHP, you can use the
DateTime
andDateTimeZone
objects:There are some date/time libraries available that provide additional options for managing dates. A popular one is the
Carbon
library (needs to be installed first; for instance, usingcomposer
). In fact, Carbon extends off of and adds additional methods and functionality to PHP’sDateTime
and other related date/time/timezone classes:A third, and in my opinion the most powerful option, is to use the ICU
INTL
extension (short for Internationalization; the extension needs to be activated in the PHP.ini file but comes bundled with PHP by default). This library is fast and powerful. It used to be disabled by most web hosting early on but has matured and functions in modern PHP are even being deprecated and removed in favor of theirINTL
equivalents. Most web hosts now have this extension enabled as it’s used by an increasing number of libraries for translation, tokenization, transliteration, formatting, etc.You saw that correctly, it will even translate to the user’s language as well as use the date/time format commonly expected by users of the region (using the locale variable) and convert the given
DateTime
object’s information to the given timezone when formatting… all without touching the default timezone. You can use any of the supported locales.