calculate end date from duration - php

I have a duration drop down menu with options 'yearly/quarterly/monthly' and start date textbox with datepicker. There is another textbox end_date.
Based on the duration I need the end date when duration and start date is selected.
E.g.: Start date is 23-09-2015 and duration chosen is 3 months so, I need to display 23-12-2015.But I have no idea how to do that.

PHP (5.3+) has a neat class to help adding a duration to a date its called DateInterval. (it has many other nice interval features as well)
$date = new DateTime('2015-10-02');
$date->add(new DateInterval('P3M'));
echo $date->format('Y-m-d'); // 2016-01-02
In the example above i add 3 months to my date. You can do other more complex things like P4Y1M2D, which is 2 days, 1 month and 4 years....
See php doc for more information: DateInterval
For a tutorial go here: Add a duration or interval to a date

Try this. This is an example. I hope this will help.
<?php
$date = date("Y-m-d");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +3 month");
echo date("Y-m-d",$date);
?>
If you need to add months and date do like this
$date = strtotime(date("Y-m-d", strtotime($date)) . " +3 month 2 day");

use
$date = date_create('2015-09-30');
date_add($date, date_interval_create_from_date_string('3 Months'));
echo date_format($date, 'Y-m-d');

Please check this
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
$(document).ready(function(){
$("#start_date").removeClass('hasDatepicker').datepicker(
{
changeMonth: true,
changeYear: true,
dateFormat: 'yy-mm-dd',
onSelect: function(selectedDate)
{
var d = new Date( selectedDate );
if($('.date_range').val() == 'Yearly')
{
d.setFullYear( d.getFullYear( ) + 1 );
}
if($('.date_range').val() == 'Quarterly')
{
d.setMonth( d.getMonth( ) + 3 );
}
if($('.date_range').val() == 'Monthly')
{
d.setMonth( d.getMonth( ) + 1 );
}
year = d.getFullYear();
month = ("0" + (d.getMonth() + 1)).slice(-2);
date = ("0" + d.getDate()).slice(-2);
end_date = year +'-'+month+'-'+date;
$('#end_date').val(end_date);
}
});
});
</script>
<select name="date_range" class="date_range">
<option value="None">Please select a date range</option>
<option value="Yearly">Yearly</option>
<option value="Quarterly">Quarterly</option>
<option value="Monthly">Monthly</option>
</select>
<p>Start Date</p>
<input type="text" name="start_date" id="start_date" />
<p>End Date</p>
<input type="text" name="end_date" id="end_date" />

Related

Date passed from input type date seems to be empty and outputs ..1970-01-01

I have a table .I want to update input type date to the the table data.I want to pass this to a php form intermediate.php .But null value seems to be get passed and output seems to be 1970-01-01 which implies data is not getting passed.
<form method="" action="intermediate.php">
<table id="updatetable">
</table>
</form>
//Jquery added here
<script>
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = yyyy + '-' + mm + '-' + dd;
var i;
for(i=0;i<5;i++){
updatetabledata +=
'<td><input type="date" id="vazhipadudate"'+i+'"
name="vazhipadudate' + i + ' " min="' + today + '"/>
</td>';
}
$("#updatetable").empty().append(updatetabledata);
</script>
Here is the php code snippet of intermediate.php
for($i=0;$i<5;$i++){
$date='vazhipadudate'.$i;
if(!empty( $_POST["$date"] ))
{/*echo "\n Date is ..".$_POST["$date"];*/
$time = strtotime($_POST["$date"]);
//$storecart[$i]['date']= $_POST["$date"];$storecart[$i]['date']
$time = strtotime($_POST["$date"]);
echo "The time now is.....".$time;
$storecart[$i]['date'] = date('Y-m-d', $time);
echo "Selected Date is..........".$storecart[$i]['date'] ;
}
}
Output is if I comment out if(!empty( $_POST["$date"] ))
The time now is.....Selected Date is..........1970-01-01
The time now is.....Selected Date is..........1970-01-01
The time now is.....Selected Date is..........1970-01-01
The time now is.....Selected Date is..........1970-01-01
The time now is.....Selected Date is..........1970-01-01
I made few changes in main file.
Add method:post, in js passing the today date in value attribute.
Now it works fine
index.php
<form method="post" action="intermediate.php">
<table id="updatetable">
</table>
<input type="submit" name="">
</form>
//JQuery Link here...
<script>
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = yyyy+ '-' + mm + '-' + dd;
var i;
var updatetabledata = "";
for(i=0;i<5;i++){
updatetabledata += '<td><input type="date" id="vazhipadudate' + i + '" name="vazhipadudate[]" min="' + today + '" value="' + today + '"></td>';
}
$("#updatetable").empty().append(updatetabledata);
</script>
intermediate.php
<?php
for($i=0;$i<count($_POST['vazhipadudate']);$i++){
echo "The time now is.....".date("Y-m-d").' ';
echo "Selected Date is..........".$_POST['vazhipadudate'][$i].'<br/>' ;
}
?>
Output
The time now is.....2020-07-10 Selected Date is..........2020-07-14
The time now is.....2020-07-10 Selected Date is..........2020-07-21
The time now is.....2020-07-10 Selected Date is..........2020-07-12
The time now is.....2020-07-10 Selected Date is..........2020-07-13
The time now is.....2020-07-10 Selected Date is..........2020-07-15

How to disable certain days of the week on a jQuery datepicker based on a result in a database with PHP

I have a mySQL database with a table of staff information and what days of the week they work i.e Tuesday, Wednesday, Friday.
I have an HTML from which allows user to book an appoint, the idea is based on who they want the appointment with would depend what days they can chose from within the jquery datepicker.
HTML
<form action="" method="post">
<input type="text" id="Name" name="Name" required="required" value="Name">
<select id="SelectStylest" name="stylest">
`<?php
$sql = mysqli_query($con, "SELECT Name FROM staff") or die(mysqli_error());
while ($row = $sql->fetch_assoc()){
echo "<option value='".$row['Name']."'>".$row['Name']."</option>";
}
?>
</select>
<input type="text" name="appointment_date"id="datepicker">
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
$( function() {
$( "#datepicker" ).datepicker();
$('input.timepicker').timepicker({
minTime: "09:00",
maxTime: "17:00"
});
});
I know I need to following query to search the database to friend the name of the stylist, but after that I get lost. I have googled the problem but haven't found anything on the subject.
$stylest = $_POST['stylest'];
$query = mysqli_query($con,"SELECT * FROM staff WHERE Name LIKE '%$stylest%'");
You can do this with the beforeShowDay option in date picker. For example this enables only Tuesdays Wednesdays and Fridays:
workingDays = [2, 3, 5];
$( "#datepicker" ).datepicker({
beforeShowDay: function (date) {
return [workingDays.indexOf(date.getDay()) > -1];
}
});
Day of week as returned by Date go from Sunday=0 to Saturday=6.
To transform your current working_days format to the numeric workingDays array needed in JS, you can use:
<?php
... // get row from mysqli_fetch_assoc
$weekDays = explode(',', $row['working_days']);
$days = [];
foreach ($weekDays as $d) {
if ($d == 'Sunday') $days[] = 0;
if ($d == 'Monday') $days[] = 1;
// ... and so on
}
echo '<script type="text/javascript">workingDays='.json_encode($days).'</script>';
?>
this will disable weekend days and a select list of holidays
var disabledDays = ["2018-11-22","2018-11-23","2018-12-24","2018-12-25", "2019-01-01"]
$( "input#ship_date" ).datepicker({
changeMonth: true,
changeYear: true,
dateFormat: "yy-mm-dd",
numberOfMonths: 1,
beforeShowDay: function(date){
var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
var day = date.getDay();
return [ day != 0 && day != 6 && disabledDays.indexOf(string) == -1 ]
}
});

Disable specific week day according to the day number(0,1,2,....,6)

I'm trying to disable all weekdays in JQuery UI datepicker and keep one day on and not disabled according to the day number that i write in the textbox nb.
Is there a way to do that?
My code is:
<p>day number: <input type="text" id="nb"></p>
<p>Date: <input type="text" id="datepicker"></p>
Notice about day number :
sunday is day 0, monday is day 1 ,..., wednesday is day 3,.....
FOR EXAMPLE : if i write 3 in the textbox nb, so i want only to turn wednesday on in the datepicker
You need to use beforeShowDay method
$('#datepicker').datepicker({
beforeShowDay: function(date) {
var weekDay = parseInt($('#nb').val(), 10);
if (weekDay === date.getDay()) {
return [true, '', ''];
}
return [false, '', ''];
}
});
https://jsfiddle.net/x8jcd48c/

how to block past dates in php and in input?

<input id="datepicker" name="datepicker" class="calendar" />
Here is my script:
<script>
$(function() {
$( "#datepicker" ).datepicker({ minDate: 0});
});
</script>
here php
if( $date < strtotime( date('m/d/Y', $time )) ) {
echo "<script>alert('You can't put past day..'); </script>"; }
I want to block the users enter past date wrongly or as know..but it does not work..i cant block..how can i block users enter past dates? i want only users can enter today n next days..so i want to filter with input in php.
strtotime() doesn't take a format string, the function you want to use is DateTime::createFromFormat. It returns a DateTime object, not a timestamp, so you need to compare it with a DateTime:
$date = $_POST['datepicker'];
if (DateTime::createFromFormat('m/d/Y', $date) < new DateTime()) {
echo "<script>alert('You can't put past day..'); </script>";
} else {
// code to insert into database
}
DEMO
i fixed n maybe it help any one who need like me..
<input id="datepicker" name="datepicker" class="calendar" /><br />
<script>
$(function() {
$( "#datepicker" ).datepicker({ minDate: 0});
});
</script>
$year=$_POST['year'];
$date=$_POST['datepicker'];
$timer=$_POST['timer'];
$time=time();
$start_data = date('m/d/Y', time() - (86400));
$now=date('m/d/Y', $time);
if( $date < $start_data ){
echo "<script>alert('Failed..'); </script>";
}
thanks for ur help again..

Date Calculation

i have a form with a fix date and time entry. (eg: 23:30 - 04/14/2011)
i have a second time entry. (eg: 00:15 - the Next Day)
how can i calculate the Date of the second entry time.
with only on these 3 variables.
Time A = 23:30 on the 04/14/2011
Time B = 00:15 on the ____ ?
Can anyone show me the semantics of this calculation how to find the Date of Time B!
Thanks
I can see a couple of ways of doing this. I would probably do something like this:
In HTML (missing form controls, labels etc):
<input name="date_from" type="text"> <!-- use some sort of javascript date popup control to format this properly-->
<input type="text" name="hours_to"> <!-- remember to add validation to input -->
<input type="text" name="mins_to"> <!-- remember to add validation to input -->
<select name="day_to">
<option value="0">The same day</option>
<option value="1">The next day</option>
<option value="2">The day after that</option>
</select>
Then in your PHP form handler, assuming date_from is in a valid date format:
$time_from = strtotime($_POST['date_from']);
$time_to = str_to_time('Y-m-d 00:00:00', $time_from) //start of the day
+ 86400 * (int) $_POST['day_to'] //86400 seconds in a day
+ 3600 * (int) $_POST['hour_to'] //3600 seconds in an hour
+ 60 * (int) $_POST['min_to']; //60 seconds in a minute
$sql_from = date("Y-m-d H:i:s", $time_from);
$sql_to = date("Y-m-d H:i:s", $time_to);
Then you can do your db insert, subject to validations etc of course.
Also, strtotime() can accept relative time parameters (e.g. '+ 1 day') which might be another way of doing this, check the PHP manual for more info
var date1 = '23:30 - 04/14/2011';
var date2 = '00:15 - the Next Day';
var d1month = date1.split(' - ')[1].split('/')[0];
var d1day = date1.split(' - ')[1].split('/')[1];
var d1year = date1.split(' - ')[1].split('/')[2];
var d1hour = date1.split(' - ')[0].split(':')[0];
var d1min = date1.split(' - ')[0].split(':')[1];
var d2hour = date2.split(' - ')[0].split(':')[0];
var d2min = date2.split(' - ')[0].split(':')[1];
var d = new Date(d1year, parseInt(d1month)-1, d1day, d1hour, d1min, 0, 0);
var d2 = new Date(d1year, parseInt(d1month)-1, parseInt(d1day)+1, d2hour, d2min, 0, 0);

Categories