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

Dan's avatar
Level 11

SQLite date column != MYSQL date column?

Hey guys,

Not sure how to handle this one, if at all possible?

MySQL handles date columns as expected i.e. just 01/01/2015. However SQLite seems to want to include times as well so the same record in SQLite looks something like 01/01/2015 00:00:00.000000

So when I'm using methods such as firstOrNew, it's not able to find the record that is already there (hope that makes sense). Are there any workarounds for this? Should I be using a MySQL database for testing?

Cheers in advance!

0 likes
6 replies
bobbybouwmann's avatar
Level 88

One quick solution would be using a datetime field in both databases.

The second solution would be always inserting a full datetime field, if mysql has a date format it will automatically stripe the time

$request->merge([
    'date' => Carbon::createFromFormat('d/m/Y', $request->get('date')),
]);

User::firstOrNew($request->all());
2 likes
Dan's avatar
Level 11

@bobbybouwmann Thanks for the reply! I'm kinda stuck with the date field (for now). I like your thinking behind your second solution, however I don't think MySQL stripes the time like you say? I've tested it out but it still doesn't seem to be finding the record that I know already exists. Am I missing something?

Cheers!

thomaskim's avatar

You could set the time to 00:00:00.

Both of these should work:

Carbon::createFromFormat('d/m/Y', $request->get('date'))->setTime(0,0);
Carbon::createFromFormat('d/m/Y', $request->get('date'))->startOfDay();
1 like
Dan's avatar
Level 11

Dammit my mistake. You guys are brilliant. Especially thanks for the tip with ->startOfDay() @thomaskim !

1 like
Dan's avatar
Level 11

@thomaskim It's a shame you can't assign more than 1 answer as correct! Thanks for your help.

Please or to participate in this conversation.