Framework: The format separator does not match with Carbon when using MS Sql Server

Created on 7 Jul 2016  路  7Comments  路  Source: laravel/framework

I am using MS SQL Server as my back end server and Larave 5.2 as the framework.

Every time I use toArray() method on eloquent collection I get this error

The format separator does not match

I am NOT overriding the getDateFormat method in any of my models.

But, I also tried to override the using the following trait

trait SqlServerDateTime {

    /**
     * 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);
    }


}

Most helpful comment

I figured out how to fix this issue

This is what I have done. I created a BaseModel Class

<?php 
namespace Vendor\MyApp;

use Illuminate\Database\Eloquent\Model as EloquentModel;

class BaseModel extends EloquentModel 
{

    public function getDateFormat()
    {
        return 'Y-m-d H:i:s.u';
    }

    public function fromDateTime($value)
    {
        return substr(parent::fromDateTime($value), 0, -3);
    }

}

Then for each model I had I extended my BaseModel class something like this

 class User extends BaseModel 
 {
 ...
 ...
 }

I hope this helps someone else

All 7 comments

I figured out how to fix this issue

This is what I have done. I created a BaseModel Class

<?php 
namespace Vendor\MyApp;

use Illuminate\Database\Eloquent\Model as EloquentModel;

class BaseModel extends EloquentModel 
{

    public function getDateFormat()
    {
        return 'Y-m-d H:i:s.u';
    }

    public function fromDateTime($value)
    {
        return substr(parent::fromDateTime($value), 0, -3);
    }

}

Then for each model I had I extended my BaseModel class something like this

 class User extends BaseModel 
 {
 ...
 ...
 }

I hope this helps someone else

Thanks, man, works like a charm

Have you discovered the cause of this problem?

I don't want to create a base model, only to fix a bug. I'll think in a better solution.

This also appears to be an issue in 5.3. Creating a BaseModel class as suggested above works. Thanks!

This shouldn't have been closed as it's a workaround, not a fix? I'm seeing this behaviour with MSSQL and need to maintain compatibility with other DB drivers.

I was able to "fix" the problem by changing the created_at and updated_at column data. The last three digits must be 000. Exemple: 2017-11-21 17:28:54.933 must become 2017-11-21 17:28:54.000.

Encountered this issue in 5.5. As a workaround for writing to storage:
$now = Carbon::now('UTC')->toDateTimeString().'.000';

Edit: I was on version 5.5.19, this seems to be fixed as of 5.5.22.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JamborJan picture JamborJan  路  3Comments

iivanov2 picture iivanov2  路  3Comments

Fuzzyma picture Fuzzyma  路  3Comments

shopblocks picture shopblocks  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments