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

346
Vistas
Laravel 5.3 - Carbon Date - UTC offset get timezone name

I am trying to get a timezone name from a UTC offset in Laravel 5.3 using Carbon. Code listed below any help would be much appreciated.

/* current code iteration */
$utcOffset = -5;
$timezone = Carbon::now($utcOffset)->timezone->getName();
echo $timezone;
// Result: -05:00
// Expected Result: EST

/* tried code */
$timezone = Carbon::now($utcOffset)->tzName;
// Result: -05:00

/* What I used prior to Carbon */
$timezone = timezone_name_from_abbr(null, $utcOffset * 3600, TRUE);
$dateTime = new DateTime();
$dateTime->setTimeZone(new DateTimeZone($timezone));
$timezone = $dateTime->format('T');'

What am I missing? I feel daft..

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Preface:

The accepted answer works in most cases but as mentioned in the user contributed notes area of timezone_name_from_abbr(), there are issues with using the function, like returning false instead of actual timezone and returning a "historical" (i.e. deprecated) timezone identifier rather than the current standard one for a given location. Which are still valid to this date.

Also, the original code returns the value as expected, as long as you know that as per Carbon docs, if you look at https://carbon.nesbot.com/docs/#api-timezone

the original name of the timezone (can be region name or offset string):

One more thing to note here is that, it is considered not reliable to derive timezone off of offset value as it does not take into consideration the DST observed periods offset.

So, this all to actually say that deriving timezone off of offset is not always possible.

Answer:

But since the OP mentioned Carbon and timezone based on offset, as per the Carbon docs as of now, the answer should be

$date = Carbon::now('-5');
echo $date->tzName;
about 4 years ago · Santiago Trujillo Denunciar

0

Tried updating Carbon to no evail ended up using the old datetime class.

$timezone = timezone_name_from_abbr(null, $utcOffset * 3600, TRUE);
$dateTime = new DateTime();
$dateTime->setTimeZone(new DateTimeZone($timezone));
$timezone = $dateTime->format('T');
about 4 years ago · Santiago Trujillo Denunciar

0

In a new Carbon it is timezoneName property;

$now = Carbon::now(-5);
echo $now->timezoneName;
//or 
echo $now->timezone->getName();
about 4 years ago · Santiago Trujillo 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