Adding time field to the current date field using Carbon

10,292

You have to change format,

Carbon::createFromFormat('Y-m-d', $date);

Will get only the year, month and day of your input, but you need also hours, minutes (and maybe seconds). So you have to change your code like that:

Carbon::createFromFormat('Y-m-d H:i:s', $date);

Anyway, i think it's easier for you if you set the $dates attribute in your model, have a look at the official documentation:

/**
 * The attributes that should be mutated to dates.
 *
 * @var array
 */
protected $dates = ['deleted_at'];

If you add your column to this array will be automatically returned a Carbon\Carbon object.

Update:

In your form you have 2 inputs: date and time, so you have 2 options:

  • Like I was saying in the comment, do not use a form builder
  • Merge these 2 inputs in a single Carbon object...

So your code has to be changed from:

$task -> end_date = $request->input('date');

to:

$task -> end_date = $request->input('date') . ' ' . $request->input('time');

And your mutator has to be changed as described above.

Share:
10,292
Dax
Author by

Dax

Updated on June 27, 2022

Comments

  • Dax
    Dax almost 2 years

    Problem: I'm making some sort of to-do application in Laravel, so for each to-do a user creates there will be a given time from the user to indicate when this to-do has to be completed. Now I have 2 input fields, 1 for the date and 1 for the specific time. I want to add the time input to the date input and when that is completed I want it to store in the database

    Question: How do I add my time field value to my date value?

    Code:

    The input fields:

    {{Form::date('date', \Carbon\Carbon::today(),['class' => 'form-control mb-2 mr-sm-2 mb-sm-0'])}}
    {{Form::time('time', \Carbon\Carbon::now()->timezone('Europe/Brussels')->format('H:i'),['class' => 'form-control'])}}
    

    My store function when the user wants to add a task:

     public function store(Request $request)
     {
    
        $this ->validate($request, ['title' => 'required|max:25','description' => 'required|max:60']);
    
        $task = new Task;
        $task -> title = $request->input('title');
        $task -> description = $request->input('description');
        $task -> end_date = $request->input('date');
        $task -> user_id = auth()->id();
        $task->save();
    
        return redirect('/tasks')->with('success', 'Task created');
     }
    

    My Task.php model where I try to manipulate the 'end_date' attribute:

        public function setEndDateAttribute($date) 
        {
    
        $this->attributes['end_date'] = Carbon::createFromFormat('Y-m-d', $date);
    
        }
    

    The result I'm getting: Date is being added to the database, but with above function I get the date + current time of creating the task. I want to replace current time with my input value of time

  • Dax
    Dax over 5 years
    Is it possible for you to show me how I can add my time in to the end_date attribute? createFromFormat('Y-m-d H:i:s', $date); this isn't adding my time I want, my time is a different input field so I need somehow to add it in
  • IlGala
    IlGala over 5 years
    Well in my opinion, first of all you shouldn't use a form builder... use raw HTML with a datetimepicker and, and blade features like Componets & slots Anwyway I'll update my answer!
  • Kyobul
    Kyobul over 4 years
    As far as I understand the idea is to work with Carbon Instance, and no to store raw "date / time" input in the database, so this does not answer the question.
  • IlGala
    IlGala over 4 years
    @Kyobul attributes in $dates array are automatically mutated to DateTime / Carbon instances objects...