1

I have what seems to be a relatively simple issue that has caused more problems than I thought it would. As you probably know, HTML5 no longer supports the "datetime" input for a form field, and only supports "datetime-local". When I try to insert "datetime-local" into my database through a form on my website, I obviously get an error because of the extra character included in "datetime-local". What I am trying to do, and why I need a datetime field as opposed to just a date and/or time in their own respective fields is because I want to use Carbon to display my datetime in different formats. How can I insert datetime through HTML form into mysql table without manually inserting the value into my database?

EDIT: Here is all of the relevant code that I am trying to achieve this with. I am using Laravel to build this application

game/create form:

<select name="season_id">

        @foreach ($seasons as $season)

        <option name="season_id" value="{{ $season->id }}">{{ $season->season }} {{ $season->year }}</option>

        @endforeach

</select>

<label for="inputDateTime" class="sr-only">DateTime</label>
    <input type="datetime-local" name="gdatetime" id="inputDateTime" class="form-control" placeholder="DateTime" required autofocus>

<label for="inputOpponent" class="sr-only">Opponent</label>
    <input type="text" name="opponent" id="inputOpponent" class="form-control" placeholder="Opponent" required autofocus>

<label for="inputLocation" class="sr-only">Location</label>
    <input type="text" name="location" id="inputLocation" class="form-control" placeholder="Location" required autofocus>

<label for="inputField" class="sr-only">Field</label>
    <input type="text" name="field" id="inputField" class="form-control" placeholder="Field" required autofocus>

game controller:

$game = Game::create(request(['gdatetime', 'opponent', 'location', 'field', 'season_id']));

Also, in my Game model, I have defined this:

protected $dates = ['gdatetime'];
2
  • Is this a user input time/date or is it for record keeping? Commented May 22, 2017 at 18:05
  • It's a user input datetime @GrumpyCrouton Commented May 23, 2017 at 0:52

3 Answers 3

1

You can always parse your datetime inputs to carbon instance and store them in the database. Parsing will take care of the headache that comes with formatting the input before storing. You can then display them in any format whenever needed.

$date = \Carbon\Carbon::parse($request->input('datetime'));

Edit : Based on your comments and update, do this.

$data = request(['opponent', 'location', 'field', 'season_id']);

$data['gdatetime'] = \Carbon\Carbon::parse(request('gdatetime'));

$game = Game::create($data);
Sign up to request clarification or add additional context in comments.

6 Comments

I get an error for an undefined variable 'request' when I try this
@hoolakoola that was an example on how to use form input. You can replace $request->input('datetime') with a datetime value or how you fetch your form input.
Okay when I change it to $date = \Carbon\Carbon::parse('gdatetime'); I get another error, DateTime::__construct(): Failed to parse time string (gdatetime) at position 0 (g): The timezone could not be found in the database
@hoolakoola please understand that you need to pass the datetime value. So if you have $gdatetime with some value then pass it like $date = \Carbon\Carbon::parse($gdatetime); You need to pass the input which holds the value. Why don't you add your current code to your question? You clearly lack some basic knowledge.
Okay I have updated the question with all the relevant code.
|
1

Use PHP to convert it.

date('date format here', strtotime($user_input);

date()

strtotime()

And always be sure to validate user input server side, otherwise strtotime() may return false causing date() to return 1969.

Comments

0

Or you can define this on your model file:

protected $dates = ['your_date_field];

and when you get it from database it will be a Carbon instance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.