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

katifrantz's avatar

MYSQL - DATETIME SAVED TO DATABASE AUTOMATICALLY BACKDATES AFTER A FEW MINUTES TO CURRENT DATE AND TIME

Hey Guys

So I have this Laravel app I just deployed to production, and when I maybe create a schedule, it records the correct time and date for that task to the database, but after a few minutes, the time and date automatically change to today's date. Its crazy, but I don't know whatz going on. Google, for the first time didn't help me. Maybe it's because I have no idea what is going on. Any ideas ? Thanks for any contributions

0 likes
10 replies
jimmck's avatar

@katifrantz First MySQL does not do this. No database does this. Second how are you saving the date? What is is the database type? NOT the Laravel Type.

Look at the data in the database with a tool like Sequel-pro or DB Visualizer or MYSql command time tool.

How are you looking at the date in Laravel?

Something is modifying your data.

katifrantz's avatar

Thing is, it works PERFECTLY ON MY LOCAL MACHINE . PERFECTLY . No problems at all.

I'm saving the date as datetime.

I used MYSQL command line to check out database, it saves correctly, and then later updates.

I feel better now knowing the error might be coming from my code, means its fixable.

But I don't know , ALL DATETIME fields in my database behave like this, and I can't have errors everywhere .

Any ideas ?

Thanks

:)

kehator's avatar

Let's see.. local machine -> works fine... hosting ( I assume ) -> pulling date.now.. It would be funny if there is some fallback to date.now if the main function doesn'rt work :)

How are you creating and updating this time? can you show us some code?

katifrantz's avatar

This is all done with Laravel, and it looks something like this:


public function saveTask() {
    $task = Task::create([
        //other stuff
        'deadline' => '2017-02-13 11:00:00'
    ]);
}

There is a field called published, and is by default 0. When I create task, all works fine. But when I call the publish method to publish task, it goes crazy and backdates to today's date.


public function publishTask($id) {

    $task = Task::find($id);
    
    $task->published = 1;
    $task->save();

    return redirect()->back();

}

At this point, as soon as I call the publishTask method, on page refresh, database entry for deadline changes to today's date, and the task is published.

That's all I got. Okay, gladly welcoming any ideas. Thanks y'allz for your time

chrisebner's avatar

hey,

just a thought, but:

does this also happen when you return $task; from publishTask() instead of return redirect()->back();

and what is the value of $task->deadline in that case?

*edit: wrong markdown syntax...

katifrantz's avatar

Hey, @chrisebner I did what you asked, by adding return $task, and strangely, it returns the $task object as a json, and this time around, the deadline field stays unchanged. That's really strange, but How can this be explained please ?

Christofer's avatar

Idea: check your model event handlers... any code being run for "saving" or "updating"? Check the model and all parent models.

PS: "and strangely, it returns the $task object as a json"... that is normal Laravel behaviour, I think the toString() is returning JSON by default. Most people want the JSON, it's better than an entire object.

Snapey's avatar

Sounds like just invoking $task->save() is setting to the current time.

What is the default value for the column in the database?

Are there any eloquent model events setup?

Do you have an accessor for this field?

I'm inclined to think that its something like an accessor. When you save the record it writes the entire attributes back to the database, not just the published flag, so if you had an accessor for deadline, Eloquent might be getting the current time instead of the real value because of some fault in your accessor

katifrantz's avatar

Hey @Snapey , @Christofer , @antiomic , @chrisebner thanks a lot for your contributions . They all helped me figure it out.

The problem was tricky, I still don't understand it fully yet. I was using a $table->timestamp('deadline'); field in my table schema. I passed Carbon date strings directly when saving records, and in some way they backdated as mentioned above. So i switched to $table->dateTime('created_at');, and everything worked as expected. Looks like these two in mysql do not have much of a difference, but looks like using dateTime for storing specific records is much preferred.

Also, I think my Mysql timezone was off , compared to my local timezone, so that caused a little bit of confusion too. Thanks for all your contributions. If you have any more ideas about this, please share :)

Please or to participate in this conversation.