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

kirchaj's avatar

Date - Form - Carbon confusion

Newbie here having a challenge getting my date field to do what I want it to. The create form works properly. The challenge comes with the edit form. I followed Jeffery's video on the topic but he skips the edit/update field formatting saying he would have to revisit later.

  1. The edit form displays the date and time and I only need the date in mm/dd/yyyy format.
    2.the html datepicker works on the create form because the field is blank. Is there a way to get the datepicker to work on the edit field while displaying the date already entered? 3.The edit date field ends up storing 0000-00-00

any assistance would be appreciated.

create view 

<div class="form-group col-sm-6 col-lg-4">
    {!! Form::label('birthdate', 'Birthday:') !!}
    {!! Form::input('date', 'birthdate', null, ['class' => 'form-control']) !!}
</div>

edit view 

<div class="form-group col-sm-6 col-lg-4">
    {!! Form::label('birthdate', 'Birthdate:') !!}
    {!! Form::text('birthdate',null, ['class' => 'form-control']) !!}
</div>

update function in controller
public function update($id, UpdatestudentRequest $request)
    {
        /** @var student $student */
        $student = student::find($id);
        $rules = array(
            "studentId" => "required | numeric | unique:students,studentId,".$id,
            "ssn"       => "required | numeric | unique:students,ssn,".$id,
            "epsbId"    => "required | numeric | unique:students,epsbId,".$id,
            "first"     => "required",
            "last"      => "required",
            "gender"    => "required", 
            "ethnicity" => "required",
            "email"     => "email",
            "birthdate" => "date"
            );
        $this->validate($request, $rules);
        $student->studentId     = $request['studentId'];
        $student->epsbId        = $request['epsbId'];
        $student->ssn       = $request['ssn'];
        $student->first         = $request['first'];
        $student->middle        = $request['middle'];
        $student->last      = $request['last'];
        $student->maiden        = $request['maiden']; 
        $student->email         = $request['email'];
        $student->gender        = $request['gender'];
        $student->ethnicity = $request['ethnicity'];
        $student->birthdate     = $request['birthday'];
        $student->notes         = $request['notes'];

        $student->save();

        return \Redirect::route('students.index')->with('message', 'Student was updated successfully.');
    }

0 likes
5 replies
bimalshah72's avatar

@kirchaj

Regarding : The edit date field ends up storing 0000-00-00 typo mistake in controller

$student->birthdate     = $request['birthday'];

It should be

$student->birthdate     = $request['birthdate'];

Also are you using form model binding in edit page?

bimalshah72's avatar
Level 6

@kirchaj

If you always need date in mm/dd/yyyy format your need to write accessors in the model

public function getBirthdateAttribute($value)
    {
        return Carbon\Carbon::parse($value)->format('m/d/Y')
    }

Best option would be make your birthdate field carbon instance by adding in model

protected $dates = ['birthdate'];

and hence you can format anywhere

kirchaj's avatar

Thanks bimalshah72. Your tip pointed me in the right directions. I still had some issues to iron out but did so with adding accessor and mutator for birthdate

public function getBirthdateAttribute($value)
        {
            $format = 'm/d/Y';
            return Carbon\Carbon::parse($value)->format($format);
        }
public function setBirthdateAttribute($value)
    {
        // Mutators (or setters) shouldn't return a value
            $format = 'm/d/Y';
                $this->attributes['birthdate'] = Carbon::createFromFormat($format, $value);
    }

I still have to do more testing, validation, etc. but I think everything is good now.

Thanks so much TK

uxweb's avatar

@kirchaj

I think that the date format belongs to the presentation layer (frontend), then a good datepicker js lib will let you give format to the date displayed.

As i can see in your code, you are using HTML5 date input field, this input only displays a date that is in yyyy-mm-dd, if you throw your date field from your model into this input, it will not display the date. This happened to me and waste lot of time scratching my head until i knew about it.

The issue was that when a carbon object is converted to string, it gives you a date formatted like yyyy-mm-dd hh:mm:ss.

The html date input field does not recognize this format.

What to do?

Creating the accesor method is good if you are not going to use it as a Carbon object, otherwise, you are losing all the functionality it gives you.

You can use the laracasts/presenter package to create a presenter for your model, where you output the date formatted as yyyy-mm-dd without the time.

class StudentPresenter
{
    public function birthday()
    {
        return $this->entity->birthday->format('Y-m-d');
    }
}

Then in your view:

<div class="form-group col-sm-6 col-lg-4">
    {!! Form::label('birthdate', 'Birthday:') !!}
    {!! Form::input('date', 'birthdate', $student->present()->birthday, ['class' => 'form-control']) !!}
</div>

Now, using Chrome it will format the date to the user system's regional settings.

For example, if a student has a birthdate of 1985-10-13 00:00:00 stored in the database, the presenter will put in the view just 1985-10-13, and if i am viewing the page from my computer configured with a region of Mexico where most people speaks spanish and dates are formatted like dd-mm-yyyy i will see the date like 13-10-1985.

If i change the date, it will be sent as yyyy-mm-dd without doing anything, this will keep out of my application how the dates are displayed to the client and stick to the database ISO standard date format.

Thing get a little messy when you want to have a javascript datepicker, i suggest you to try http://amsul.ca/pickadate.js/date/, this is a real good library to display a date picker for desktop or mobile, and have flexible configuration options.

Check it out and if you have more answers please reply.

I had a hard time to figure it out by myself but i learned a lot and i would like to help you and learn more.

kirchaj's avatar

uxweb,

Thanks so much for your explanation. I actually understand it :) and can make this work for me. In this case there is only one date I need to worry about but I have others where the form could have 20 dates. I tried to watch Jeffrey's video on presenters but it is still a little over my head.

I am just going to take these learning opportunities one at a time and keep moving forward.

Thanks again TK

Please or to participate in this conversation.