skip to Main Content

I’m trying to save a date in a database, I’m posting the correct value which for example is Thu Oct 13 2022 15:00:00 GMT+0300 (Eastern European Summer Time) and then, the date that gets passed to the controller converts to 2022-10-13T12:00:00.000Z this is obviously a timezone issue, although in app.php the timezone i have set is 'timezone' => 'Europe/Athens' which is correct and it is GMT +3, what could the issue be?

2

Answers


  1. By default, timestamps are formatted as ‘Y-m-d H:i:s’. If you need to customize the timestamp format, set the $dateFormat property on your model. This property determines how date attributes are stored in the database, as well as their format when the model is serialized to an array or JSON. Please check below example And your date format may be "E MMM d yyyy HH:mm:ss Z"

    <?PHP
    
     namespace App;
    
     use IlluminateDatabaseEloquentModel;
    
     class Flight extends Model
     {
      /**
        * The storage format of the model's date columns.
        *
        * @var string
      */
      protected $dateFormat = 'U';
    }
    
    Login or Signup to reply.
  2. by default, the timestamps table in your database will have an ISO_8601 format, which you see has Z at the end which mean Zero Hour Format or Zulu Time (UTC)

    so the date you pass which is Thu Oct 13 2022 15:00:00 GMT+0300 is equal to 2022-10-13T12:00:00.000Z in UTC format.

    you can easily convert the date to any format you want when parsing on the front-end, or you can do attribute casting

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search