Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TrederusMaximus's avatar

Heart of the Date Format

Hello all,

I have to work with MS SQL Server which means that I need to change the date format from "Y-m-d H:i:s" to "Y-d-m H:i:s" .

I have found the Model function getDateFormat():

    protected function getDateFormat()
    {
        return $this->dateFormat ?: $this->getConnection()->getQueryGrammar()->getDateFormat();
    }

and then followed it down to "getDateFormat()":

    /**
     * Get the format for database stored dates.
     *
     * @return string
     */
    public function getDateFormat()
    {
        return 'Y-d-m H:i:s';
    }

But if I change the format here that does not seem to have any influence of the date format. I know that there is a way by overriding the getDateFormat() funtion in the model but as I would have to do that for every model and also have other code that I've just required via composer that relies on the underlying date format structure I'd like to change it at it's very heart.

Any ideas and/or experience what would be the best way?

Thanks in advance! :-)

0 likes
9 replies
giovanniciriello's avatar

I really advice you to use Carbon PHP library. It is included into Eloquent, you have just to define the date (or datetime, or time) field in your database in your model class:

class Order extends Model
{

    protected $dates = [
        'date',
        'created_at',
        'updated_at'
    ];
/**/

}

In this way you will directly get from your queries a object of Carbon Class, and so:

$order->date->toDateTimeString(); //2016-10-14 12:17:51

And make it easy :)

TrederusMaximus's avatar

Laravel is using Carbon but that relies on the SqlServerGrammar.php that implements a funtion getDateFormat();

That's the way it looks within SqlServerGrammar.php:

    /**
     * Get the format for database stored dates.
     *
     * @return string
     */
    public function getDateFormat()
    {
        return 'Y-m-d H:i:s.000';
    }

I've changed that to my required format and then it wrote the data into the DB. But now I am having problems getting the data out! This comes due to Carbon not being able to parse the incoming timestamp.

ErrorException in Carbon.php line 425:
Trailing data (View: C:\cw\PhpstormProjects\asweb\resources\views\users\index.blade.php)

Now I'm considering changing the format in the DB but after looking into that for some time now I'm also not sure if that's an easy fix.

TrederusMaximus's avatar

Okay... changing the format in the DB is no real option. That means I need to find a way to get it done through Laravel. Can't I just say somewhere Carbon this: 'Y-d-m H:i:s' is your date format for all inputs and all outputs?

petrit's avatar

You can add a helper function which will get date in "Y-m-d H:i:s" and return date "Y-d-m H:i:s".

TrederusMaximus's avatar

I have tracked the problem down to the DB. MS SQL Server accepts date formats in the format 'Y-d-m H:i:s' and outputs them in the format 'Y-m-d H:i:s'. What is that?! Even if that exceeds the scope of Laravel - I'd be happy for any hints on how to make the accepted in- and output formats in MSSQL equal!

TrederusMaximus's avatar
Level 4

Thanks to every single one that contributed to this thread. I finally got it working and I will document my solution here in order to help people with the same problem. I did the following:

  • Go to the MS SQL SERVER DB#
  • Create a new Login
  • Set the default language for this login to the desired language. This way you do not have to set the language settings globally and you can also set the default DB for your new login such that you only work with your DB in the language you require. In laravel I did a global search using PHP_Storm and changed all the format strings to 'Y-m-d H:i:s.u'. Make sure you do not forget the very last '.u' as I got the error message "trailing data" without that.

Now Laravel and MS SQL Server have the same date format and I can continue my work!

PS: Go to config/database.php and add

'pooling' => false,

to your MS SQL SERVER-DB connection definition if you get an invalid DB handle returned after chaning to the new login unser!

1 like
henry_rios's avatar

Thank you so much @TrederusMaximus !! I've been dealing with the problem for a long time without any solution until now. My login user was set in Spanish, so I created a new one in english and problem solved.

1 like

Please or to participate in this conversation.