Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

334
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!