samedi 18 août 2018

timestamp changing without modifyng the field in the db

I'm having a problem with my 'end_date' column on my database. First off, I'm making a To-do application. So I have a table called 'Tasks' with following Columns:

$table->increments('id');
$table->integer('user_id');
$table->string('title');
$table->mediumText('description');
$table->timestamp('end_date');
$table->integer('completed');
$table->timestamps();

When a user creates a task they can give an end_date to this task. I'm in the UTC+2 timezone so for the user I'm displaying all times in UTC+2 but i'm storing them in UTC on my database.

Here are the input fields with the actual Store function:




Because I'm using 2 input fields I have to merge them together in the store function:

 public function store(Request $request)
{
     $requestData = $request->all(); 
     $task = new Task($requestData);
     $inputDateAndTime = $requestData['end_date'].' '.$requestData['time'].':00';
     $task['end_date'] = Carbon::createFromFormat('Y-m-d H:i:s', $inputDateAndTime, 'Europe/Brussels')->timezone('UTC');
     $task['user_id'] = auth()->id();
     $task['completed'] = 0;
     $task->save();

    return redirect('/tasks')->with('success', 'Task created');
}

In this function I specify that the user has given a Time in UTC+2 timezone and I want this to be converted in UTC. (This works as expected)

The problem: So if the user wishes to complete this task, they can do a PUT request to update the 'completed' column, But when this request is being handled my end_date column is being changed on the same time without touching the column at all.

My update function:

public function update(Request $request, Task $task)
{
       $task -> completed = 1;
       $task->save();

       return redirect('/tasks')->with('success', 'Task completed');
}

Example of the problem:

Let us say that user 1 creates a task with following end date = 08/18/2018 20:14 Note that this time is for UTC+2, so I'm saying convert this to UTC.

After the store function, I check the database, with following end_date = 2018-08-18 18:14:00 Which is correct, now lets see what my end_date is when I run the update function:

public function update(Request $request, Task $task)
{
    dd($task->end_date);

    // the result:

    Carbon @1534616040 {#552 ▼ date: 2018-08-18 18:14:00.0 utc(+00:00)
}
}

So still the same value, but If I run the store function as normal and check the database this is my value: 2018-08-18 17:24:27, what I noticed is that my updated_at field has the same last 2 digits, so it is somehow taking the current time I would assume. My updated_at field: 2018-08-18 15:24:27

Any idea what I'm doing wrong here?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire