Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
How to display proper time base on user timezone?

Description:

I build this web app for my son as his newborn gift 🎁 last year. My wife & I are obsessed with it since. We use it extensively every day until now on our phones, tablets, laptops, TVs, and even our Samsung fridge (required only internet & browser). I will release this to ALL parents in the world soon* for FREE. But now I want to allow my nephew & niece in Texas to use this dashboard/web app as a family test. I have to support diff timezone. Right now it only supports US ET which is where I live.

enter image description here

enter image description here

JJ's Dashboard

Desktop View (Read-only)

https://mybabies.app/baby/a4b7efe4-9c2f-4c48-b5b1-cc516a8f027f?code=N0Vk90

Setup:

My config/app.php current set to 'timezone' => 'America/New_York', I use moment.js a lot in the front-end to display my entire date/time logic.


I was thinking:

to take the input as +2, +3, and so on.... so I can render the time based on that. I know I have to take user timezone as input and store that into the database as : +2, +3, ... . I've done that. I can also query back what user set as well. I just don't know what to do with that TZ. I'm afraid that I will change my codes in tons of places..

Is there a way to just detect user TZ just like we detect user dateTime? Should I consider that route instead ?

What are the proper steps I should take?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think very optimistically... I got a timezone to work on my app now. Huge thanks to all the comments from everyone above, without you guys, I will never found the best working solution for my situation with such minimum changes.

Front-end

First, I added 2 lines of code, I grab the client timezone

 const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; <<< ADDED : Line #1

Since I have already made an ajax on log pressed anyway, I just need to one more field to the server

var data    = {};
data.tz     = timezone; <<< ADDED : Line #2 

Back-end

Second, with 5 lines of code, I can access what I sent from FE like so to overwrite the created_at time.

$tz     = $inputs['tz']; <<< ADDED : Line #3 

$clientDate = new DateTime("now", new DateTimeZone($tz) ); <<< ADDED : Line #4 
$clientTimestamp = $clientDate->format('Y-m-d H:i:s'); <<< ADDED : Line #5 

$log             = new BabyLog;
$log->type       = $type;
$log->created_at = $clientTimestamp; <<< ADDED : Line #6 
$log->updated_at = $clientTimestamp; <<< ADDED : Line #7 
$log->save();

Now

My database stored the right timestamp() based on my client device/browser.

No other codes needed, it just works.


Test

I changed the Date & Time on my iPhone from Chicago to New York back and forth. I pressed log, it saved, and I noticed my front-end displayed the accurate time based on my client timezone now.

enter image description here

I hope

this post helps someone like me that wants to make their web-app work globally with minimal code changes. To be very specific in 7 lines of codes.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda