Select date range based upon previous date - php

I'm having 2 textbox for date. One is From Date & another is To Date.
Let us say.
From Date 1
To Date 1
&
From Date 2
To Date 2
I am using date picker for textboxes. If user select 24-01-2012 in 'From Date 1' & 24-01-2016 in 'To Date 1'. Then, I've to restrict user to select date in 'From Date 2' from 01-01-1935 -To- date selected in 'From Date 1'.
<?
$current_year = date("Y");
$past_year = $current_year - 80;
?>
I'm storing date as
var FromDate1= $("#FromDate1").val();
var ToDate1= $("#ToDate1").val();
var FromDate2= $("#FromDate2").val();
var ToDate2= $("#ToDate2").val();
Here, $past_year and $current_year is available. How can I restrict user to select date in 'From Date 2' from 01-01-1935 -To- 'FromDate1'.
$(".addmorefromP").datepicker({
changeYear: true,
maxDate: new Date(),
changeMonth: true,
yearRange: '<?php echo $past_year . ':' . $current_year; ?>',
dateFormat: 'mm-dd-yy',
onClose: function () {
$(this).valid();
}
});
Here, yearRange: '<?php echo $past_year . ':' . $current_year; ?>', is used. Is there any way to use FromDate1 in place of $current_year.
I have been trying since 2 hours. I am not good in jquery, so i was unable to do it. Any help/hint is appreciable. Please help.

Please have a look at below code. May be it will be helpful.
Fiddle Link: http://jsfiddle.net/pqhtt7gb/5/
HTML:
<div id="date1">
FromDate1: <input id="FromDate1" data-index="0" class="datepickerStart" type="text" />
ToDate1: <input id="ToDate1" data-index="0" class="datepickerEnd" type="text" />
</div>
<div id="date2">
FromDate2: <input id="FromDate2" data-index="1" class="datepickerStart" type="text" />
ToDate2: <input id="ToDate2" data-index="1" class="datepickerEnd" type="text" />
</div>
JQuery Code:
$(".datepickerStart").datepicker({
constrainInput: true,
changeMonth: true,
changeYear: true,
firstDay: 1,
numberOfMonths: 1,
onClose: function (selectedDate, obj) {
var index = obj.input.data("index");
$(".datepickerEnd[data-index="+index+"]").datepicker("option", "minDate", new Date(selectedDate));
if(index == 0){
$(".datepickerStart[data-index=1],.datepickerEnd[data-index=1]").datepicker("option", "maxDate", new Date(selectedDate));
}
}
});
var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var pastYear = currentYear - 80;
var pastDate = new Date(pastYear+"/01/01");
$(".datepickerStart[data-index=1]").datepicker("option", "minDate", pastDate);
$(".datepickerStart[data-index=1]").datepicker( "setDate", pastDate);
$(".datepickerEnd").datepicker({
constrainInput: true,
changeMonth: true,
changeYear: true,
firstDay: 1,
numberOfMonths: 1
});

Try this
$(".addmorefromP").datepicker({
changeYear: true,
maxDate: new Date(),
changeMonth: true,
minDate:new Date(<?php echo $past_year;?>),
maxDate:new Date(<?php echo $current_year;?>),
//yearRange: '<?php echo $past_year . ':' . $current_year; ?>',
dateFormat: 'mm-dd-yy',
onClose: function () {
$(this).valid();
}
});

Related

How do I display the end date 31-03- year(depending upon the start_date)?

I have a two date picker. In the first date picker is a start date and second date picker is an end date.
My year range is
2018-2019 (Last date is 31-03-2019)
2019-2020 (My start date is 01-04-2019 and the end date is 31-03-2020)
2020-2021 (My start date is 01-04-2020 and the end date is 31-03-2021)
//and so on
Now according to the current year, we are in the range of 2018-2019.
So my end date will be the 31-03-2019.
How do I display the end date 31-03- year(depending upon the start_date)?
Should I need some jquery or PHP to handle this?
$(function() {
var year = (new Date).getFullYear();
$(".start_date").datepicker({
showOn: "button",
buttonImage: "http://jqueryui.com/resources/demos/datepicker/images/calendar.gif",
buttonImageOnly: true,
buttonText: "Select date",
dateFormat: "dd-mm-yy",
changeMonth: true,
changeYear: true,
minDate: 0,
//maxDate : "+1Y",
maxDate: new Date(year, 03, 31),
// maxDate: "+3m"
showAnim: "clip",
numberOfMonths: 1
});
$(".end_date").datepicker({
showOn: "button",
buttonImage: "http://jqueryui.com/resources/demos/datepicker/images/calendar.gif",
buttonImageOnly: true,
buttonText: "Select date",
dateFormat: "dd-mm-yy",
changeMonth: true,
changeYear: true,
minDate: 0,
//maxDate : "+1Y",
maxDate: new Date(year, 03, 31),
showAnim: "clip"
//numberOfMonths: 2
});
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<div class="form-group">
<label>Start Date</label>
<input type="text" name="start_date" placeholder="Start Date" id="start_date" class="start_date form-control">
</div>
<div class="form-group">
<label>End Date</label>
<input type="text" name="end_date" id="end_date" placeholder="End Date" class="end_date form-control">
</div>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
Would you help me out in this?
You can, using onSelect option, set the maxDate in the .end_date datepicker.
$(function() {
var year = (new Date).getFullYear();
$(".start_date").datepicker({
showOn: "button",
buttonImage: "http://jqueryui.com/resources/demos/datepicker/images/calendar.gif",
buttonImageOnly: true,
buttonText: "Select date",
dateFormat: "dd-mm-yy",
changeMonth: true,
changeYear: true,
minDate: 0,
//maxDate : "+1Y",
maxDate: new Date(year, 03, 31),
// maxDate: "+3m"
showAnim: "clip",
numberOfMonths: 1,
onSelect: function(dt, dp) {
var selected = $.datepicker.parseDate("dd-mm-yy", dt);
var yy = selected.getFullYear();
var mm = selected.getMonth();
var dd = selected.getDate()
var nextYear = $.datepicker.parseDate("dd-mm-yy", "31-03-" + (yy + 1));
$(".end_date").datepicker("option", "maxDate", nextYear);
}
});
$(".end_date").datepicker({
showOn: "button",
buttonImage: "http://jqueryui.com/resources/demos/datepicker/images/calendar.gif",
buttonImageOnly: true,
buttonText: "Select date",
dateFormat: "dd-mm-yy",
changeMonth: true,
changeYear: true,
minDate: 0,
//maxDate : "+1Y",
maxDate: new Date(year, 03, 31),
showAnim: "clip"
//numberOfMonths: 2
});
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<div class="form-group">
<label>Start Date</label>
<input type="text" name="start_date" placeholder="Start Date" id="start_date" class="start_date form-control">
</div>
<div class="form-group">
<label>End Date</label>
<input type="text" name="end_date" id="end_date" placeholder="End Date" class="end_date form-control">
</div>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
Since we get the date in string, we can use $.datepicker.parseDate() to parse it into a Date and increment the year into a new Date.
Hope that helps.

Start from x date for calendar within form driven by php, jquery and html

We've been tasked with editing a form on a website to start from a particular date in June. It currently starts on today's date. Obviously once the specified date has passed, it will then need to start from the day's date again.
The first part of the jQuery on the current form is below:
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<!--/jQuery UI-->
<script>
jQuery(function ($) {
var dateFormat = "dd/mm/yy";
$("#check_in").datepicker({dateFormat: dateFormat, minDate: '0d', onSelect: function (dateStr) {
var date = $(this).datepicker('getDate');
if (date) {
date.setDate(date.getDate() + 1);
var formattedDate = new Date(date);
var d = formattedDate.getDate();
var m = formattedDate.getMonth();
m += 1;
var y = formattedDate.getFullYear();
var minDate2 = d + "/" + m + "/" + y;
$("#check_out").val(d + "/" + m + "/" + y);
}
},
onClose: function (selectedDate) {
var date2 = $('#check_in').datepicker('getDate');
date2.setDate(date2.getDate() + 1);
$("#check_out").datepicker("option", "minDate", date2);
}}).datepicker("setDate", new Date());
$("#check_out").datepicker({dateFormat: dateFormat, minDate: '+1d'}).datepicker("setDate", "+1D");
});
jQuery(document).ready(function () {
jQuery(".bookingSubmit").click(function (e) {
e.preventDefault();
window.location.href = "https://subdomain.maindomain.com/#/?city=" + jQuery("#city").val() + "&check_in=" + jQuery("#check_in").val() + "&check_out=" + jQuery("#check_out").val() + "&page=1";
});
});
</script>
The php segment is below:
<div class="form-control">
<!--<label for="city">Check-In:</label>-->
<input type="text" name="check_in" id="check_in" />
</div>
<div class="form-control">
<!--<label for="city">Check-Out:</label>-->
<input type="text" name="check_out" id="check_out" />
</div>
<div class="form-control">
<!--<label for="city"></label>-->
<input type="submit" value="Search" class="bookingSubmit" />
</div>
The final part of the jQuery script is below:
<script>
jQuery(function () {
var opts = {
defaultDate: "+1w",
showWeek: true,
firstDay: 1,
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
dateFormat: "d MM yy",
altFormat: "mm/dd/yy",
onClose: function (addDays) {
var dateArr = jQuery('#start').datepicker('getDate');
var dateLeav = jQuery('#end').datepicker('getDate');
if (!(dateArr < dateLeav)) {
dateArr.setDate(dateArr.getDate() + 2);
jQuery('#end').datepicker('setDate', dateArr);
}
;
}
};
jQuery('#start').datepicker(
jQuery.extend({
altField: '#from, .arrivalDate'
}, opts)
);
jQuery('#end').datepicker(
jQuery.extend({
altField: '#to, .leavingDate'
}, opts)
);
jQuery('.arrivalDate').datepicker(
jQuery.extend({
altField: '#start'
}, opts)
);
jQuery('.leavingDate').datepicker(
jQuery.extend({
altField: '#end'
}, opts)
);
var queryDate = new Date('06/04/2018');
var end_date = new Date('06/06/2018');
var today_date = new Date();
if (today_date <= queryDate) {
jQuery("#start").datepicker('setDate', queryDate);
jQuery("#end").datepicker('setDate', end_date);
}
else {
jQuery("#start").datepicker('setDate', '+0d');
jQuery("#end").datepicker('setDate', '+2d');
}
});
</script>
Any help or pointers greatly appreciated. Thanks.

How to set End Date plus one from Start Date using jQuery DatePicker

I have successfully integrate jQuery DatePicker on my site and wondering how can I set the following:
Don't allow selection of start date from current date plus 2 days. example if today's date is 7/20/12 then the visitor can select only date starting 7/22/12.
End date must start based on Start Date plus one. If start date is 7/23/12 then the end date should be 7/24/12.
BTW, I am making a hotel reservation calendar.
Here's my code based from sample:
<script>
$(function() {
$( "#sd" ).datepicker();
});
$(function() {
$( "#ed" ).datepicker();
});
</script>
<tr>
<td>Check In</td>
<td>:</td>
<td><input name="sd" type="text" id="sd" value="<?php echo $_SESSION['checkin'] ?>" size="10"" maxlength="8" /></td>
</tr>
<tr>
<td>Check Out</td>
<td>:</td>
<td><input name="ed" type="text" id="ed" value="<?php echo $_SESSION['checkout'] ?>" size="10" maxlength="10" /></td>
</tr>
You can do it inside your datepicker events.
$(function() {
$( "#sd" ).datepicker({
// before datepicker opens run this function
beforeShow: function(){
// this gets today's date
var theDate = new Date();
// sets "theDate" 2 days ahead of today
theDate.setDate(theDate.getDate() + 2);
// set min date as 2 days from today
$(this).datepicker('option','minDate',theDate);
},
// When datepicker for start date closes run this function
onClose: function(){
// this gets the selected start date
var theDate = new Date($(this).datepicker('getDate'));
// this sets "theDate" 1 day forward of start date
theDate.setDate(theDate.getDate() + 1);
// set min date for the end date as one day after start date
$('#ed').datepicker('option','minDate',theDate);
}
});
$( "#ed" ).datepicker();
});
please check Jquery: DatePicker: start/end date for answer.
Long story short :
<script type="text/javascript">
$(document).ready(function () {
$('#endDate').datepicker({ showOn: 'button',
buttonImage: '../images/Calendar.png',
buttonImageOnly: true, onSelect: function () { },
onClose: function () { $(this).focus(); }
});
$('#startDate').datepicker({ showOn: 'button',
buttonImage: '../images/Calendar.png',
buttonImageOnly: true, onSelect:
function (dateText, inst) {
$('#endDate').datepicker("option", 'minDate', new Date(dateText));
}
,
onClose: function () { $(this).focus(); }
});
});

jQuery DatePicker, how to show value in input field from database

can't find solution to this issue. So i have datepicker input field, it works fine, i can choose date and save it to database. But when i want to edit this saved value in edit_profile.php this field is empty, i need to see the user specified date. How to do it?
Datepicker function:
$(function() {
$( "#datepicker" ).datepicker({
dateFormat: "yy-mm-dd",
changeMonth: true,
changeYear: true
}).val('<?php echo $age;?>');
});
Input:
<input name="datepicker" type="text" id="datepicker" class="date_picker" value="" />
what to do ?
Can't you just do this:-
<input name="datepicker" type="text" id="datepicker" class="date_picker" value="<?php echo $age;?>" >
I think you should use defaultDate option, for example
$( "#datepicker" ).datepicker({ dateFormat: "yy-mm-dd", changeMonth: true, changeYear: true, defaultDate: yourDate });
You can use SetDate or Defaultdate option of datepicker:
$('#dateselector').datepicker("setDate", new Date(2008,9,03) );

Default date formatting

How do I change defaultDate: "+1w" to this format: yyyy-mm-dd? It must display like that in the input field.
JavaScript here:
<script>
$(function() {
var dates = $( "#from").datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
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 );
}
});
});
</script>
php code here:
<?php
$IMG_DIR_URL = "./digits/";
$date = date("d F Y");
?>
Displaying contents here:
<div class="field">
<label>Date</label>
<input type="text" name="date" value="<?php value('date'); ?>" id="from" />
<?php if(isset($errors['date'])) { ?>
<div class="error"><?php echo $errors['date']; ?></div>
<?php } ?>
</div>
use date('Y-m-d') for yyyy-mm-dd with the PHP date function.
you can format it in the datepicker options by adding dateformat: 'yy-mm-dd', like so:
$(function() {
var dates = $( "#from").datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
dateformat: 'yy-mm-dd',
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 );
}
});
});
I don't know what your value function does, but if it doesn't show the date
your PHP for displaying it should be:
<input type="text" name="date" value="<?php echo $date; ?>" id="from" />

Categories