how to block past dates in php and in input? - php

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

Related

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

Issue with checking if current date is bigger than what's stored

So the issue currently is the code works fine, however the second the month resets id goes from May to June the code will no longer work because It's looking for if it's greater, and as 1st is not greater than 31st it has an issue, it's not looking at the month or year, just the day..
EXAMPLE: Database stores 31/05/2016 as $reward.
In this example $current would output at 03/06/2016
<?php
$reward1 = str_replace('/', '-', $reward);
$stored = date('d/m/Y', strtotime($reward1. ' + 1 days'));
$current = date('d/m/Y');
if ($reward < $current) { ?>
<script type="text/javascript">
$(window).load(function(){
$('#redeem1').modal('show');
});
</script>
<?php } } ?>
I think there is a few errors in your code as posted above in comments.
See if it works.
$reward1 = str_replace('/', '-', $reward);
$stored = strtotime(date('d-m-Y', strtotime($reward1. ' + 1 days')));
$current = strtotime(date('d-m-Y'));
// now both values are in integer
// using d/m/y is not a standard datetime, php assumes it is american m/d/y.
if ($stored < $current) { ?>
<script type="text/javascript">
$(window).load(function(){
$('#redeem1').modal('show');
});
</script>
<?php } } ?>
You are comparing strings. This is wrong.
Take a look at the excellent Carbon library for the best practice way to achieve this. https://github.com/briannesbitt/Carbon
$reward = Carbon::parse($reward);
$now = Carbon::now();
if ($reward->lt($now)) {
// Do something
}

disable date in jquery datepicker after some time

I've created a datepicker for one of my client.
They are in a flower delivery business and they offer same day delivery for order which is placed before 12 noon. so I created a following code for that. The basic overview of its functionality is:
-> Disable current date if the time is greater than 12:00 (noon) so that customer can not put order for same day delivery.
But somehow recently one order can at 6PM for same day delivery. Which is an issue.
This is what I did so far:
<?php
// This is how i check for current date and time and if the delivery cut off time is passed add that date to disabled dates array. so customer can not put order of the current date.
current_day = date( "w" );
$todays_cut_off_time = get_option("weekday_cut_off_time_$current_day");
$todays_cut_off_time = strtotime($todays_cut_off_time);
$current_time = date( 'H:i', current_time( 'timestamp', 0 ) );
$current_time = strtotime($current_time);
if($todays_cut_off_time < $current_time)
{
$disabled_dates_array[] = date("m/d/Y");
}
?>
<script>
jQuery( document ).ready(function() {
var disableddates = <?php echo $disabled_delivery_dates; ?>; //contains list of disabled dates by admin.
exceptions = <?php echo $holiday_exception_dates; ?>; //contains dates those are exceptional
function DisableSpecificDates(date) {
var m = date.getMonth() + 1;
if(m <= 9)
m = '0'+m;
var d = date.getDate();
if(d <= 9)
d = '0'+d;
var y = date.getFullYear();
// First convert the date in to the mm-dd-yyyy format
// Take note that we will increment the month count by 1
var currentdate = m + '/' + d + '/' + y ;
// We will now check if the date belongs to exceptions array
for (var i = 0; i < exceptions.length; i++) {
// Now check if the current date is in disabled dates array.
if (jQuery.inArray(currentdate, exceptions) != -1 ) {
return [true];
}
}
// We will now check if the date belongs to disableddates array
for (var i = 0; i < disableddates.length; i++) {
// Now check if the current date is in disabled dates array.
if (jQuery.inArray(currentdate, disableddates) != -1 ) {
return [false];
}
}
// In case the date is not present in disabled array, we will now check if it is a weekend.
// We will use the noWeekends function
//var weekenddate = jQuery.datepicker.noWeekends(date);
var day = date.getDay();
return [(<?php echo $disabled_week_day; ?>), ''];
}
function FindMinDate(date){
return 0;
}
jQuery( "#delivery_date" ).datepicker({
dateFormat: 'mm-dd-yy',
minDate: 0,
beforeShowDay: DisableSpecificDates
});
});
</script>
Can somebody please guide me in the right direction to get this done. Thanks in advance.

calculate end date from duration

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" />

inserting php value into javascript

I have a countdown script i wan to key in the targetdate as a value in a form to store in the database.i have tried targetdate = $date in the script but it doesn't seems to read the value.
<script language="JavaScript">
TargetDate ="1/31/2012 5:00 AM";
CountActive = true;
CountStepper = -1;
LeadingZero = true;
DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
FinishMessage = "It is finally here!";
</script>
<script language="JavaScript" src="http://scripts.hashemian.com/js/countdown.js"></script>
<script type="text/javascript">
var TargetDate = <?php echo json_encode($date); ?>;
</script>
Depending on the format of $date if it's a timestamp you could also try
<script type="text/javascript">
var TargetDate = <?php echo json_encode(date('j/n/Y g:i a', $date)); ?>;
</script>
obviously you can't use it dinamycally as the json_encode will be parsed only before the onload javascript event , so if you need to modify it dinamically from server you should update it with ajax or getJson call

Categories