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

nscott's avatar

Date reverts to 1970-01-01 00:00:00 instead of correct date value

I have a form set up to edit a selected date range if the user needs to. When I check the value of the dates they show that the correct old date is being pulled from the database. However, if I only change one of the dates in the form and hit save, the date that is not changed reverts to 1970-01-01 00:00:00. From what I have read this is because the value is null. So why is my date showing up but not being transferred into the database again?

Here is my controller update function:

 public function update(Request $request, $id)
    {
         $this->validate($request, [
            'date' => 'min:3 | nullable',
            'second_date' => 'min:3 | nullable',
            'holiday_name' => 'min:3 | nullable',
            'hours' => 'min:3 | nullable',
        ]);

        $holiday = Holiday::find($id);
        // dd($holiday);

        $holiday->date = date('Y-m-d ', strtotime($request['date']));
        $holiday->second_date = date('Y-m-d', strtotime($request['second_date']));
        $holiday->holiday_name = $request->input('holiday_name');

        $holiday->update();

        return redirect()->route('holiday.index');
    }

And my form in the view is this:

<form class="form--admin" action="{{ route('holiday.update', $holiday->id)}}" method="post" enctype="multipart/form-data">
	@method('PATCH')
	@csrf
	<fieldset>
		<div class="formrow">
			<div class="formitem">
				<label for="date" class="label">Date: {{ $holiday->date->format(' F jS ') }}</label>
				<input type="date" value="{{ old('date', $holiday->date) }} " name="date" id="date"></input>
			</div>
		</div>
		<div class="formrow">
			<div class="formitem">
				@if(empty($holiday->second_date))
				<label for="second_date" class="label">Select a second Date </label>
				@else
				<label for="second_date" class="label">Date: {{ $holiday->second_date->format('F jS ') }}</label>
				@endif
				<input type="date" value="{{ $holiday->second_date }} " name="second_date" id="second_date"></input>
			</div>
		</div>
		<div class="formrow">
			<div class="formitem">
				<label for="holiday_name" class="label">Holiday Name</label>
				<input type="text" value="{{ $holiday->holiday_name }} " name="holiday_name" id="holiday_name"></input>
			</div>
		</div>
		<a href="{{ route('holiday.index', ) }}" class="button btn--cancel" role="button">Cancel</a>
		
		<button class="primary button" type="submit">Save Changes</button>
		<a href="{{ route('showroom')}} " class="button btn-edit" target="_blank">View</a>
	</fieldset>
</form>
0 likes
7 replies
jlrdw's avatar

Timestamp or date, you must use date for a date field, not a timestamp. Look up yourself unix timestamp.

1 like
nscott's avatar

In my database, it is formated that way though. So not sure I follow.

jlrdw's avatar
jlrdw
Best Answer
Level 75

In the database the date should be:

YYYY-MM-DD

That's how MySql stores dates. What format do you have,

Usually

$tdate = new \DateTime($somedate);  got from input
$ndate = $tdate->format('Y-m-d');

$ndate gets stored. If you have a database where the date format is not correct, fix that.

1 like
nscott's avatar

So I guess maybe I am just confused as to why the dates won't update. When the dates are created all I do in the controller is pass the input value in and it works just fine like this:

$holiday = new Holiday;

        $holiday->date = $request->input('date');
        $holiday->second_date = $request->input('second_date');
        $holiday->holiday_name = $request->input('holiday_name');

        $holiday->save();

However, I do the same thing on the update form and I would expect it to either keep the old dates if nothing is selected or change the date to. But if I do not actually select a new date it wipes out the old date. If I inspect the input form in the browser I can see that the old value is there:

value="2021-05-12 00:00:00"

The update function looks like this:

 $holiday = Holiday::find($id)

        $holiday->date = $request->input('date');
        $holiday->second_date = $request->input('second_date');
        $holiday->holiday_name = $request->input('holiday_name');
        // dd($holiday);
        $holiday->update();

So why is the date getting wiped out? It should not be wiped out, it should stay what it already is set to unless it is changed. Does that make sense?

jlrdw's avatar

When you edit, if it's not in the correct format, again format it correctly prior to saving.

1 like
nscott's avatar

Ahhh, I am so sorry. I totally missed that is what you meant. So because the value in the form has the time at the end, it has to be removed in order for it to match what is in the database. That 100% makes sense. Sorry I just misread that I changed that format and works like a charm. Thanks.

Please or to participate in this conversation.