Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

333
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda