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

shariff's avatar
Level 51

MySQL automatically changes date to previous date

Hi all,

I am going through a weird situation in a Laravel inertia application. I am not understanding where the problem actually is. For example when I'm passing

date : 2022-04-27

when I try to insert in the table at 12AM - 12:59AM in mid night automatically date changes to

date : 2022-04-26

In other time it is working perfectly. only the time between 12:00AM to 12:59AM. Please let me know where I'm going wrong.

thank you

0 likes
10 replies
jlrdw's avatar

Are you saving in UTC? I don't think it's mysql doing that.

MohamedTammam's avatar

I believe it's about the time zone, check the client/server time zones and see the differences.

Tray2's avatar

Show us the code in your controller, model, view and migration.

shariff's avatar
Level 51

@Tray2

my controller

$reservation =  $customer->reservations()->create([
            'date' => Carbon::parse($request->date)->format('Y-m-d'),
]);

migration

 Schema::create('reservations', function (Blueprint $table) {
            $table->id();
            $table->date('date');
            $table->timestamps();
        });

model

 protected $fillable = [
        'date',
    ];
Tray2's avatar

@shariff There is a difference between timestamp and date

DATETIME vs TIMESTAMP The biggest difference between these are how far back in time you can go. DATETIME can be set to the first of January of year 1000 while TIMESTAMP start from the first of January 1970. TIMESTAMP has an upper limit of 2038 while DATETIME has the upper limit of 9999. The TIMESTAMP has a higher precision, and takes consideration of the current timezone while DATETIME doesn't.

So try changing to timestamp insead of date, if you care about the timezone.

https://tray2.se/posts/database-design-part-2

shariff's avatar
Level 51

@Tray2 yeah I'm using date data type for storing date. I just saw created_at and updated_at column of reservation table at what the time the date is getting changed. During this timestamp 2022-04-27 00:18:42 it is saving as previous date

jlrdw's avatar

@shariff I have been searching myself and nothing comes to mind yet.

Change your configuration just temporarily to store UTC, the next time you are between 12 a.m. and 12:59 a.m. and see if it stores correctly.

That is weird that is the only time it stores incorrectly.

I will continue searching for bugs, Etc.

1 like
shariff's avatar
Level 51

@jlrdw Okay I will try like that. I saw timestamp of created_at and updated_at in phpmyadmin it is showing like this 2022-04-27 00:18:42.

Please or to participate in this conversation.