In this connected world, its very common that you have users accessing or using your application or website from across the globe. Its very important to give users a good experience and showing the correct time is one of them, although it may seem a tiny dot in overall picture but showing incorrect times can impact the overall experience.
Why do i need to convert datetime — Global User base
Most common use case is where your user sitting in US inputs some data on your website or application, which is then processed by application or an administrator in the backend, the backend team could be residing in India. In such cases it becomes very important that you show all the stakeholders time as per their timezone and not from the server or users who inputted or processed the data.
The image below shows how you should you convert and save the time in your database. Idea is to –
1- Save — convert from users timezone into UTC (recommended it to save in timezone of your server), save in DB.
2- Display — convert back to user timezone from UTC, when displaying to user.
Serving one region only..
If you have an application or website that serving user only for one region and you are using CMS like WordPress, you can set the timezone to desired region by editing php.ini (need to be dedicated or cloud VM) or set in your php file (if you are on shared hosting).
Case 1- php.ini — look for date.timezone in your php.ini file and set the desired timezone.
date.timezone = Europe/London
Case 2- set in your php file. At top of your php file, which will executed at the time data insertion. Add the following
date_default_timezone_set(‘Europe/London’);
Share Hosting — no control on changing timezone on PHP settings.
If you are dedicated or cloud hosting, it is possible to go into your PHP settings and save (case 1 above), where as on shared hosting you need to set this in your PHP file (case 2 above)
Hope this help budding developers working on application serving global users. We at Varshyl Technologies help enterprise customer with going digital and create their web presence or digitalised their processes.
Entrepreneur and Technology Enthusiast | Started Varshyl Technologies, a web and mobile application development company, helping companies build and promote their digital presence. Co-founded Snapworks – a mobile first communication platform for schools. Outside VT, enjoys his morning workouts, reading biographies and golf.