Pedro Dias
Pedro Dias

Reputation: 122

How can I declare a date variable and insert the value from a datetimepicker into my database?

Previously, I was using just the datepicker function from bootstrap to insert a date value on a form that will get submitted to the database.

Now, I've changed to "Tempo Dominus Bootstrap 4" since it has some interesting functions I might want to use in the future.

Here's what I have currently done.

HTML for the date input

<div class="form-group">
                    <label for="date">Date</label>
                    <div class="input-group date" id="datetimepicker1" data-target-input="nearest">
                        <input type="text" class="form-control datetimepicker-input" id="shiftDate" data-target="#datetimepicker1"/>
                        <div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
                            <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                        </div>
                    </div>

Script to set the minDate to now

<script type="text/javascript">
        $("#datetimepicker1").datetimepicker({
            minDate: moment()
        });
</script>

Now this was how I was inserting the date when I didn't have the time included and it was working

$date=strtotime($_POST['date']);
$date=date("Y-m-d",$date);

Any help for the declaration is appreciated!

Upvotes: 0

Views: 549

Answers (1)

madalinivascu
madalinivascu

Reputation: 32354

You have a datetime picker so your date has hour, minutes & seconds so you need to create the date based on the new format

$date=date("Y-m-d H:i:s",$date);

Also make sure you name your input

<input type="text" class="form-control datetimepicker-input" name="date" id="shiftDate" data-target="#datetimepicker1"/>

Upvotes: 1

Related Questions