php - Date Time picker does not save selected date to the database
Get the solution ↓↓↓In my laravel project, I want to save Datepicker selected date to the database. But my code saves date as "01-01-1970" and not the selected date from the date picker. I tried to fix it by going through many codes. But no luck.
Here is the controller store function.
public function store(Request $request)
{
$qualification = new qualification();
$qualification->from = date("d-m-Y", strtotime($request->from));
$qualification->to = date("d-m-Y", strtotime($request->to));
$qualification->save();
return redirect()->route('qualification.index')->with('success','Data Added');
}
Here is the blade.php
<label for="date">From:</label><input type="text" name="date" value="" />
<label for="date">To:</label><input type="text" name="date" value="" />
<script>
$(function() {
$('input[name="date"]').daterangepicker({
singleDatePicker: true,
showDropdowns: true,
minYear: 1995,
maxYear: parseInt(moment().format('YYYY'),10)
}, function(start, end, label) {
var years = moment().diff(start, 'years');
});
});
</script>
Please help me to save the selected date to the database. Thank you for any help.
Answer
Solution:
You are trying to get values of $request->from and $request->to in your controller but in HTML both the text fields are named as date.
It Should be Like
HTML:
<label for="date">From:</label><input type="text" class="date" name="from" value="" />
<label for="date">To:</label><input type="text" class="date" name="to" value="" />
In js
<script>
$(function() {
$('.date').daterangepicker({
singleDatePicker: true,
showDropdowns: true,
minYear: 1995,
maxYear: parseInt(moment().format('YYYY'),10)
}, function(start, end, label) {
var years = moment().diff(start, 'years');
});
});
</script>
Answer
Solution:
You can do this
<script>
$(function() {
$('input[name="date"]').daterangepicker({
singleDatePicker: true,
showDropdowns: true,
minYear: 1901,
maxYear: parseInt(moment().format('YYYY'),10),
locale: {
format: 'DD-MM-YYYY'
}
}, function(start, end, label) {
var years = moment().diff(start, 'years');
//alert("You are " + years + " years old!");
});
});
</script>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: zsh: command not found: php
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.