auto-fill age field when date is selected from datepicker - php

I want the age field to be automatically filled as soon as you select the date from the datepicker.Right now the datepicker is not working in the snippet dont know why.I do also have the working age calculation code in the php section.Only need help in filling the age field as soon as you select the date.I read that you need ajax for that but I couldnt really work that out. Please do help as I am a beginner working on this part.
< script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js" > < /script>
<script src="https:/ / maxcdn.bootstrapcdn.com / bootstrap / 4.0.0 - alpha.2 / js / bootstrap.min.js " integrity="
sha384 - vZ2WRJMwsjRMW / 8U7i6PWi6AlO1L79snBrmgiDpgIWJ82z8eA5lenwvxbMV1PAh7 " crossorigin="
anonymous "></script>
<script src="
http: //cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>
< script type = "text/javascript" >
$(document).ready(function() {
$('#datetimepicker8').datepicker({
icons: {
time: "fa fa-clock-o",
date: "fa fa-calendar",
up: "fa fa-arrow-up",
down: "fa fa-arrow-down"
}
});
});
< /script>
<?php
$dob=$_POST['birthdate'];
$dob=explode("/",
$dob);
$age=(date("md",
date("U",
mktime(0,
0,
0,
$dob[0],
$dob[1],
$dob[2]))) > date("md") ? ((date("Y") - $dob[2]) - 1):(date("Y") - $dob[2]));
echo"age is" $age;
?>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" integrity="sha384-y3tfxAZXuh4HwSYylfB+J125MxIs6mR5FOHamPBG064zB+AFeWH94NdvaCBm8qnd" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" />
<link href="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/css/datepicker.css" rel="stylesheet" type="text/css" />
<form method="post" id="signUpForm">
<div class="container" id="signupContainer">
<div class="form-group row">
<label class="col-sm-2 form-control-label">Birthdate</label>
<div class="col-sm-5">
<div class='input-group date' id='datetimepicker8'>
<input type='text' id="birthdate" class="form-control" placeholder="D.O.B" name="birthdate" />
<span class="input-group-addon">
<i class="fa fa-calendar" aria-hidden="true"></i>
</span>
</div>
</div>
<label class="col-sm-1 form-control-label">Age:</label>
<div class="col-sm-4">
<input type="text" id="age" class="form-control" name="age" placeholder="Age" />
</div>
</div>
</div>
</form>

Try this code, php code is not required. Date of birth is calculated in datepicker onchange itself.
$('#datetimepicker8').datepicker({
icons: {
time: "fa fa-clock-o",
date: "fa fa-calendar",
up: "fa fa-arrow-up",
down: "fa fa-arrow-down"
},
dateFormat: "dd/mm/yy"
}).on('change', function (ev) {
var selectDate = $('#datetimepicker8').val().split("/");
var todayDate = new Date();
var selectedDate = new Date(selectDate[2], selectDate[1], selectDate[0]);
var firstDate = new Date(todayDate.getFullYear(), todayDate.getMonth() + 1, todayDate.getDate());
var diff = Math.floor(firstDate.getTime() - selectedDate.getTime());
var day = 1000 * 60 * 60 * 24;
var days = Math.floor(diff / day);
var months = Math.floor(days / 31);
var years = Math.floor(months / 12);
if (years > 0) {
$('#age').val(years);
} else {
$('#age').val('');
}
if (days <= -1) {
alert("Please select valid date of birth.")
$('#datetimepicker8').val("");
}
});

Related

Is there a way to make a second Input field?

Is there a way to make a second Input field so that the 1st time value is in one input field and the second is in the other? I am trying to post the times to a database so I need then in separate fields.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" />
<div class='amount-time-sch'>
<label for="amount-time">Time</label>
<input type="text" name="amount-time" id="amount-time" style="border: 0; color: #666666; font-weight: bold;" value="10:00 - 20:00"/>
<div id="slider-time"></div><br>
</div>
<script>jQuery(function() {
jQuery('#slider-time').slider({
range: true,
min: 0,
max: 1000,
step: 15,
values: [ 600, 1200 ],
slide: function( event, ui ) {
var hours1 = Math.floor(ui.values[0] / 60);
var minutes1 = ui.values[0] - (hours1 * 60);
if(hours1.length < 10) hours1= '0' + hours;
if(minutes1.length < 10) minutes1 = '0' + minutes;
if(minutes1 == 0) minutes1 = '00';
var hours2 = Math.floor(ui.values[1] / 60);
var minutes2 = ui.values[1] - (hours2 * 60);
if(hours2.length < 10) hours2= '0' + hours;
if(minutes2.length < 10) minutes2 = '0' + minutes;
if(minutes2 == 0) minutes2 = '00';
jQuery('#amount-time').val(hours1+':'+minutes1+''+hours2+':'+minutes2 );
}
});
});
</script>
Yes, you can have your jQuery write the value to two different inputs with unique ids.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" />
<div class='amount-time-sch'>
<label for="amount-time">Time</label>
<!-- <input type="text" name="amount-time" id="amount-time" style="border: 0; color: #666666; font-weight: bold;" value="10:00 - 20:00"/> -->
<input type="text" name="start-time" id="start-time" value="10:00">
<input type="text" name="end-time" id="end-time" value="20:00">
<div id="slider-time"></div><br>
</div>
<script>jQuery(function() {
jQuery('#slider-time').slider({
range: true,
min: 0,
max: 1000,
step: 15,
values: [ 600, 1200 ],
slide: function( event, ui ) {
var hours1 = Math.floor(ui.values[0] / 60);
var minutes1 = ui.values[0] - (hours1 * 60);
if(hours1.length < 10) hours1= '0' + hours;
if(minutes1.length < 10) minutes1 = '0' + minutes;
if(minutes1 == 0) minutes1 = '00';
var hours2 = Math.floor(ui.values[1] / 60);
var minutes2 = ui.values[1] - (hours2 * 60);
if(hours2.length < 10) hours2= '0' + hours;
if(minutes2.length < 10) minutes2 = '0' + minutes;
if(minutes2 == 0) minutes2 = '00';
jQuery('#start-time').val(hours1+':'+minutes1);
jQuery('#end-time').val(hours2+':'+minutes2);
}
});
});
</script>
I would advise using an html time input for each time value. Here is a hypothetical example:
<form action="#" onsubmit="sendTimes(event)">
<label for="first-time">first time</label>
<input type="time" name="first-time" id="first-time"><br>
<label for="second-time">second time</label>
<input type="time" name="second-time" id="second-time"><br>
<input type="submit" value="submit">
</form>
<script type="text/javascript">
function sendTimes(event) {
// keep browser from refreshing
event.preventDefault();
const formData = new FormData();
// get the values of the times
const firstTime = $("#first-time").val();
const secondTime = $("#second-time").val();
// add the times to the form data to POST
formData.append("first-time", firstTime);
formData.append("second-time", secondTime);
// insert logic here for sending the form data to PHP
}
</script>
The time values will be empty strings if the user leaves them empty. Otherwise, they will be 24-hour times in the form "hh:mm". On the server-side, you can validate each time value in PHP before saving them to the database.

How to edit Kendo Scheduler Popup template

I'm getting problems with Kendo Scheduler UI, i'm trying to customize the popup editor with custom fields but i cannot edit elements in the popup. I would need some help to understand how to edit fields.
I've tried different scripts from telerik support but with no results. With these scripts i can create a new popup but not edit the old one.
<div id="scheduler"></div>
<script>
$('#editasnew').click(function(){
console.log("edit now");
var scheduler = $("#scheduler").data("kendoScheduler");
scheduler.cancelEvent();
setTimeout(function(){
console.log("add new event now");
scheduler.addEvent({ title: "(No title)" });
}, 2000);
});
$("#scheduler").kendoScheduler({
date: new Date("2013/6/6"),
editable: {
template: $("#editor").html()
},
edit: function(e) {
if (!e.event.isNew()) {
$(".k-edit-buttons.k-state-default").prepend('<a class="k-button" id="editasnew">Edit as New</a>');
}
},
views: [
{ type: "day" }
],
dataSource: [
{
id: 1,
start: new Date("2013/6/6 08:00 AM"),
end: new Date("2013/6/6 09:00 AM"),
title: "Interview"
}
]
});
</script>
NO errors, just not working. basically i would like to convert the "end: new Date("2013/6/6 09:00 AM")" line to a drop-down list.
<script id="editor" type="text/x-kendo-template">
<div class="k-edit-label">
<label for="title">Title</label></div>
<div class="k-edit-field" data-container-for="title">
<input type="text" class="k-input k-textbox" name="title" data-bind="value: title"></div>
<div class="k-edit-label">
<label for="start">Start</label>
</div>
<div data-container-for="start" class="k-edit-field">
<input type="text"
data-role="datetimepicker"
data-interval="15"
data-type="date"
data-bind="value:start,invisible:isAllDay"
name="start"/>
<input type="text" data-type="date" data-role="datepicker" data-bind="value:start,visible:isAllDay" name="start" />
<span data-bind="text: startTimezone"></span>
<span data-for="start" class="k-invalid-msg" style="display: none;"></span>
</div>
<div class="k-edit-label"><label for="end">End</label></div>
<div data-container-for="end" class="k-edit-field">
<input type="text" data-type="date" data-role="datetimepicker" data-bind="value:end,invisible:isAllDay" name="end" data-datecompare-msg="End date should be greater than or equal to the start date" />
<input type="text" data-type="date" data-role="datepicker" data-bind="value:end,visible:isAllDay" name="end" data-datecompare-msg="End date should be greater than or equal to the start date" />
<span data-bind="text: endTimezone"></span>
<span data-bind="text: startTimezone, invisible: endTimezone"></span>
<span data-for="end" class="k-invalid-msg" style="display: none;"></span>
</div>
<div class="k-edit-label"><label for="ownerId">Employee's</label></div>
<div data-container-for="ownerId" class="k-edit-field">
<select id="ownerId" data-bind="value:ownerId" data-role="dropdownlist"
data-value-field="" data-text-field="">
#*#for (int i=0; i<='#ViewBag.employeelist'.length; i++){#
<option value=${' #ViewBag.employeelist[0]'EmployeeId}>${' #ViewBag.employeelist[0]'Employeename}</option>
# } #*#
</select>
#* <select class="form-control" data-role="dropdownlist" data-text-field="Employeename" data-value-field="EmployeeId" data-bind="value:EmployeeId, source: #ViewBag.employeelist"></select>*#
</div>
</script>
#(Html.Kendo().Scheduler<HRMSWebApplication.Models.Data.TaskViewModel>()
.Name("scheduler")
.Date(DateTime.Now)
.StartTime(new DateTime(DateTime.Now.Year, 12, 31))
.Height(550)
.Views(views =>
{
// views.DayView();
// views.WorkWeekView();
// views.WeekView();
views.MonthView(MonthView => MonthView.Selected(true));
//views.AgendaView();
//views.TimelineView();
})
//.Editable(e => e.TemplateId("editor"))
.Resources(resource =>
{
resource.Add(m => m.OwnerID)
.Title("Employee")
.DataTextField("Employeename")
.DataValueField("EmployeeId")
.BindTo(#ViewBag.employeelist);
})
.DataSource(d => d
.Model(m =>
{
m.Id(f => f.TaskID);
m.Field(f => f.Title);
m.RecurrenceId(f => f.RecurrenceID);
})
.Read("Read", "LeavePlanner")
//.Filter(filters =>{filters.Add(model => model.OwnerID).IsEqualTo(System.Web.HttpContext.Current.Session["userid"].ToString());})
.Create("Create", "LeavePlanner")
.Destroy("Destroy", "LeavePlanner")
.Update("Update", "LeavePlanner")
// .Filter(filters =>
// {
// //filters.Add(model => model.OwnerID).IsEqualTo(1).Or().IsEqualTo(2);
//})
)
)
This how we done in MVC.Aspnet please above script works for you

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);
});
});

How to check time overlap in bootstrap date

I want to check shift start time and shift time values overlapped or not. if user enter overlapped values it should prompt error. please advice
<div class="form-group">
<label for="">Shift Start:</label>
<div class='input-group date' id='datetimepicker3'>
<input type='text' name="shift_start" class="form-control"
id='datetimepicker3' value="{{old('shift_start')}}" /> <span
class="input-group-addon"> <span
class="glyphicon glyphicon-time"></span>
</span>
</div>
<div class="form-group">
<label for="">Shift End:</label>
<div class='input-group date' id='datetimepicker5'>
<input type='text' name="shift_end" class="form-control"
value="{{old('shift_end')}}" /> <span class="input-group-addon">
<span class="glyphicon glyphicon-time"></span>
</span>
</div>
</div>
this is my script
$(function () {
$('#datetimepicker3').datetimepicker({
format: 'HH:mm'
});
$('#datetimepicker5').datetimepicker({
format: 'HH:mm'
});
});
Create a javascript function :
function checkTime()
{
var start = $('#datetimepicker3').val();
var end = $('#datetimepicker5').val();
var tabStart = start.split(':');
var tabEnd = end.split(':');
var dateStart = new Date();
dateStart.setHours(parseInt(tabStart[0]));
dateStart.setMinutes(parseInt(tabStart[1]));
var dateEnd = new Date();
dateEnd.setHours(parseInt(tabEnd[0]));
dateEnd.setMinutes(parseInt(tabEnd[1]));
if(dateStart < dateEnd)
{
alert('ok');
}
else
{
alert('ko');
}
}
Then in you datetimepicker :
$(function () {
$('#datetimepicker3').datetimepicker({
format: 'HH:mm',
onSelect: function(){
checkTime();
}
});
$('#datetimepicker5').datetimepicker({
format: 'HH:mm',
onSelect: function(){
checkTime();
}
});
});

Display the date difference between two text field input without refreshing page?

I have two field in the form which are mention in below:-
<div class="form-group">
<label class="control-label col-sm-2" for="date">Date:</label>
<div class="col-sm-5">
<input type="text" class="form-control" name="training_start_date" value="">
to
<input type="text" class="form-control" name="training_end_date" value="">
</div>
</div>
I need when we give the date in two field then output can be get in the same page in this place:
Duration: <!-- Display the difference of those dates without refreshing -->
I am feel glad and thankful if anyone give the concept about to do this requirement.
You are looking for something like this?
HTML:
<!-- JQuery and JQuery UI -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<div class="form-group">
<label class="control-label col-sm-2" for="date">Date:</label>
<div class="col-sm-5">
<input id="date1" type="text" class="form-control" name="training_start_date" value="">to
<input id="date2" type="text" class="form-control" name="training_end_date" value="">
</div>
</div>
<br> <span> Diff:</span> <span id='diff'> - </span> <span> Days</span>
JS:
$('#date1').datepicker();
$('#date2').datepicker();
$('#date2').change(function () {
var diff = $('#date1').datepicker("getDate") - $('#date2').datepicker("getDate");
$('#diff').text(diff / (1000 * 60 * 60 * 24) * -1);
});
Working JSFiddle: http://jsfiddle.net/ddan/dLtp0k1u/
EDIT
Based on your comment:
No manual download needed here. It's just referencing hosted libraries.
Use this in a simple html page:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<!-- JQuery and JQuery UI -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
</head>
<body>
<div class="form-group">
<label class="control-label col-sm-2" for="date">Date:</label>
<div class="col-sm-5">
<input id="date1" type="text" class="form-control" name="training_start_date" value="">to
<input id="date2" type="text" class="form-control" name="training_end_date" value="">
</div>
</div>
<br>
<span> Diff:</span>
<span id='diff'> - </span>
<span> Days</span>
<script>
$('#date1').datepicker();
$('#date2').datepicker();
$('#date2').change(function () {
var diff = $('#date1').datepicker("getDate") - $('#date2').datepicker("getDate");
$('#diff').text(diff / (1000 * 60 * 60 * 24) * -1);
});
</script>
</body>
</html>
I think I have majority of your problems solved...
Change the type to 'date' for all your inputs. Like this:http://www.w3schools.com/html/tryit.asp?filename=tryhtml_input_date
Use the HTML onblur attribute so that your function runs every time the input box is out of focus.
Use the code below to calculate Date Difference. Code Credit:http://ditio.net/2010/05/02/javascript-date-difference-calculation/
var DateDiff = {
inDays: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000));
},
inWeeks: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000*7));
},
inMonths: function(d1, d2) {
var d1Y = d1.getFullYear();
var d2Y = d2.getFullYear();
var d1M = d1.getMonth();
var d2M = d2.getMonth();
return (d2M+12*d2Y)-(d1M+12*d1Y);
},
inYears: function(d1, d2) {
return d2.getFullYear()-d1.getFullYear();
}
}
var dString = "May, 20, 1984";
var d1 = new Date(dString);
var d2 = new Date();
document.write("<br />Number of <b>days</b> since "+dString+": "+DateDiff.inDays(d1, d2));
document.write("<br />Number of <b>weeks</b> since "+dString+": "+DateDiff.inWeeks(d1, d2));
document.write("<br />Number of <b>months</b> since "+dString+": "+DateDiff.inMonths(d1, d2));
document.write("<br />Number of <b>years</b> since "+dString+": "+DateDiff.inYears(d1, d2));

Categories