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

338
Vistas
Laravel 5, PgSQL and Date format

I'm struggling with Laravel date formats, using it with PgSQL.

My need :

  • input date must be in this ISO8601 format: Y-m-d\TH:i:s\Z (example: 2016-05-13T10:05:00Z)
  • output must be in the same format
  • PgSQL stores it in the format Y-m-d H:i:s (example: 2016-05-13 10:05:00)

The hard part is to configure Laravel to globally accept a particular date format in input and output, but without changing "internal" format.

A property can be overridden to customize format, but it acts globally:

protected $dateFormat = 'Y-m-d\TH:i:s\Z';

This leads to errors when retrieving dates from database (trying to convert a PgSQL date with a custom mask: InvalidArgumentException).

I would like to avoid using a Middleware or do it manually in each controller response, is there any way?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Sounds like you want to use accessors and mutators. Here's a basic example with a class that has a my_date column, the conversion might not be exactly right (I'm just assuming strtotime() will work) but that should be easy for you to fix.

class SomeModel extends Model
{
    // triggered whenever you retrieve the date from the db
    public function getMyDateAttribute($value)
    {
        return date('Y-m-d\TH:i:s\Z', strtotime($value));
    }

    // triggered whenever you insert the date into the db
    public function setMyDateAttribute($value)
    {
        $this->attributes['my_date'] = date('Y-m-d H:i:s', strtotime($value));
    }
}

Now, your database can use one datetime format and the other parts of your application can use another. If you want it to be reusable for other models, you can extract it to a trait as long as the columns are named the same.

over 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