aardalich's avatar

Timestamp/DateTime issue with 5.1 & SqlServer

Heyas

Have a production site running on Win2008 R2, IIS, SQL2012 and Laravel 5.0
Far from my ideal but forced to go where the databases are.

I updated dev to 5.1 and tried pushing that onto production today.

I'm getting the error
ErrorException in Carbon.php line 414:
The format separator does not match
The format separator does not match
The format separator does not match

Some googling points me to trying to modify the getDateFormat() method within SqlServerGrammar.php inside the vendor directory to either

  • return 'Y-m-d H:i:s+' or
  • return 'Y-m-d H:i:s.u'

which seems to get me going again, but then as soon as I try to save()

SQLSTATE[22007]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Conversion failed when converting date and/or time from character string.

and the sql error would be because of
[updated_at] = 2015-07-20 08:48:31+ where [id] = 5486

and that is about as far as I have managed to get to

0 likes
5 replies
toby's avatar
toby
Best Answer
Level 31

Hi!

I had a similar issue w/Lumen; what I did was to override the getDateFormat() method in a BaseModel class which every other model will extend like this

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class BaseModel extends Model
{
    /**
     * @return string date format for mssql
     */
    protected function getDateFormat()
    {
        return 'Y-m-d H:i:s.u0';
    }
}
1 like
aardalich's avatar

Thanks @toby

Also found the save() was solved adding to a method using the BaseModel approach I found from https://github.com/laravel/framework/pull/4181

Ended up adding these 2 methods.

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

/**
 * Convert a DateTime to a storable string.
 * SQL Server will not accept 6 digit second fragment (PHP default: see getDateFormat Y-m-d H:i:s.u)
 * trim three digits off the value returned from the parent.
 *
 * @param  \DateTime|int  $value
 * @return string
 */
public function fromDateTime($value)
{
    return substr(parent::fromDateTime($value), 0, -3);
}
larschinkel's avatar

Laravel still throws an conversion error after trying googled things and this here. I ended up modifying the File: \vendor\laravel\framework\src\Illuminate\Database\Schema\Grammars\SqlServerGrammar.php like this:

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

That fixed it for me.

daiv's avatar

I know this question has a "best answer" but this solution worked for me only when no Pivot-Table was used. I tried to use Mutators, but this wasn't possible on Pivot-Tables.

If you struggle with the datetime in MSSQL you should check the MSSQL User you use to connect to the DB

SELECT dateformat FROM sys.syslanguages where name = @@LANGUAGE;

This returned "dmy" and I needed "myd" what seems to be the default setting on MSSQL.

So try to

SET LANGUAGE 'us_english'

I hope this will help someone!

See also https://database.guide/how-to-change-the-current-date-format-in-sql-server-t-sql/

paulofreitas's avatar

This should affect any Laravel version up to 5.5. To anyone having issues with SQL Server, this should fix any issues while working with dateTime() and timestamp() columns:

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

/**
 * Convert a DateTime to a storable string.
 *
 * @param  \DateTime|int  $value
 * @return string
 */
public function fromDateTime($value)
{
    return $value->format('Y-m-d H:i:s.v');
}
2 likes

Please or to participate in this conversation.