Additional date is showing in Bootstrap datepicker - php

I am using bootstrap datepicker
here is my code
var available_Dates = ["2015-08-05","2015-08-16","2015-08-21"];
$('input.date-pick').datepicker({
language: 'en',
beforeShowDay: function(date){
var formattedDate = $.fn.datepicker.DPGlobal.formatDate(date, 'yyyy-mm-dd', 'en');
if ($.inArray(formattedDate.toString(), available_Dates) == -1){
return {
enabled : false
};
}
return;
}
});
Html i am using
<div class="form-group">
<label><i class="icon-calendar-7"></i> Select a date</label>
<input class="date-pick form-control" placeholder="Jul 18, Sat" data-date-format="M d, D" type="text">
</div>
Now the problem is, the result comes with an additional day.
2015-08-06
2015-08-17
2015-08-22
But i want selected date to be the result.
Please provide suggestion,
Thank you

Related

Date Time picker does not save selected date to the database

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.
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>
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>

handling error where date selected less than current date

I'm a newbie in this php and html. I'm developing a laundry system using php with mysql. right now, users can just enter whatever date they want for pickup and delivery.. for example, today is 10 feb 2018, user key in 9 feb 2018 as pickup. that should not be possible. how do i handle that? my html are
<font face="Arial Black">Pickup date:</font>
<input type="date" name="pickupdate">
The below snippet is from one of my projects, see if this helps you in anyway:
The HTML Code:
<label for="from">From</label>
<input type="text" id="from" name="from"/>
<label for="to">to</label>
<input type="text" id="to" name="to"/>
The Javascript Code:
var dateToday = new Date();
var dates = $("#from, #to").datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 3,
minDate: dateToday,
onSelect: function(selectedDate) {
var option = this.id == "from" ? "minDate" : "maxDate",
instance = $(this).data("datepicker"),
date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
dates.not(this).datepicker("option", option, date);
}
});

multiple datepicker in a page (php)

im trying to use two date picker in a page. it seems to worked fine but after clicked on the submit button the date entered is not the same as the choosen date. both of the date inserted to the database is 01-01-1970.
how can i fix this?
<div class="form-group">
<label class="control-label col-sm-4">Date Issued</label>
<div class="col-sm-5">
<input class="form-control" type="text" id="dateIssuedpc" name="dateissued" required class="form-control" placeholder="Enter Date Issued"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">Date Expired</label>
<div class="col-sm-5">
<input class="form-control" type="text" id="dateExpiredpc" name="dateexpired" required class="form-control" placeholder="Enter Date Expired" />
</div>
</div>
<script>
$( function() {
$( "#dateIssuedpc").datepicker({
dateFormat: "dd-mm-yy",
changeMonth: true,
changeYear: true
});
$( "#dateExpiredpc" ).datepicker({
dateFormat: "dd-mm-yy",
changeMonth: true,
changeYear: true
});
} );
</script>
if field data type in database is datetime then
$dateexpired = $_POST['dateexpired'];
$new_dateexpired = date("Y-m-d H:i:s",strtotime($dateexpired));
$dateissued = $_POST['dateissued'];
$new_dateissued = date("Y-m-d H:i:s",strtotime($dateissued));
if field data type is date then
$dateexpired = $_POST['dateexpired'];
$new_dateexpired = date("Y-m-d",strtotime($dateexpired));
$dateissued = $_POST['dateissued'];
$new_dateissued = date("Y-m-d",strtotime($dateissued));
This is not datepicker problem, rather problem is with PHP code.
Mysql date format is always YYYY-MM-DD HH:II:SS
So while inserting data to mysql table, you have to convert the format for mysql
Ex:
$postDate = $_POST['date'];// just collect value from post fields.
$date = date("Y-m-d H:i:s",strtotime($postDate));
You need to format your date properly. First of all change format from dateFormat: "dd-mm-yy", to *dateFormat: "dd-mm-yyyy",* to better recognise year.
After that from php side you something like this
$dateissued = date("Y-m-d", strtotime($_POST['dateissued']) ); //MySql date format

subtract the given age into the current date

I have two fields where one field is used to give input of user age and another field to display the date of birth as per current date.
My html code is:-
<div class="col-xs-4 form-group">
<label><?php echo _('DOB'); ?> : </label>
<input value="" type="text" name="jbs_dob" id="jbs_dob" size="17" />
</div>
<div class="col-xs-4 form-group">
<label><?php echo _('Age'); ?> : </label>
<input value="" class="validate[required] form-control" type="text" name="jbs_age" id="jbs_fname" size="17" />
</div>
My js and jquery code is:
$(function () {
$("#jbs_age").click(function () {
var jbs_dob = new Date();
var jbs_age =$('jbs_age').val();
var diff= jbs_dob-jbs_age;
$( "#jbs_dob" ).html(diff);
});
});
Try below code. This will return you Year of the birth. If you provide age 25 then it will show 1991 in birth day field.
$(function () {
$("#jbs_age").click(function () {
var jbs_dob = new Date();
var jbs_age =$('#jbs_age').val();
var age = (jbs_dob.getFullYear() - jbs_age);
$( "#jbs_dob" ).val(age);
});
});

Setting value to bootstrap datapicker object

I use bootstrap datepicker object http://eonasdan.github.io/bootstrap-datetimepicker
HTML
<div class='input-group date' id='datetimepicker1'>
<input type='text' class="form-control" value="" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
In JS file
$('#datetimepicker1').datetimepicker({
format: 'l',
locale:$.fn.getlocale(),
showTodayButton:true,
maxDate:moment().add(6, 'month')
});
On page loading, I want to set a date that is provided by mysql back end. I tried the following code and I don't see any date being shown in the UI.
var backend_date = dataset["APPLICATION_DEADLINE"];
var date = new Date(backend_date);
$('#datetimepicker1').data("DateTimePicker").date(moment(date));
Could you please help?
Try This:
var backend_date = dataset["APPLICATION_DEADLINE"];
var date = new Date(backend_date);
$('#datetimepicker1').datetimepicker({
defaultDate: date
});
I think I have found an easy way
var dateobj = $.fn.getDateObjectFromMYSQL(dataset["APPLICATION_DEADLINE"]);
$('#datetimepicker1').data("DateTimePicker").date(dateobj);
$.fn.getDateObjectFromMYSQL = function(mysqldate){
var t = mysqldate.split(/[- :]/);
var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
return d;
}

Categories