multidatespicker with disabled dates from php variable - php

I'm trying to get the holidays from a php function and using them to disable dates for my multidatepicker.
Using jquery multidatespicker from: http://multidatespickr.sourceforge.net/
//php:
$holidays = show_holidays($year, $province);
which then $holidays echo's as:
['01/01/2014','02/17/2014','04/18/2014','05/19/2014,'07/01/2014','08/04/2014','09/01/2014','10/13/2014','12/25/2014','12/26/2014']
my js:
var holidays = "<?php echo $holidays; ?>";
$('#with-altField').multiDatesPicker({
addDisabledDates: holidays,
altField: '#vacationdays',
beforeShowDay: $.datepicker.noWeekends
});
and it doesn't disable the dates...
firebug shows error : uncaught exception: Missing number at position 0

Sorry all, i figured it out... i had to put it in array and remove the [ ] from php var:
changed:
var holidays = "<?php echo $holidays; ?>";
to
var holidays = [ <?php echo $holidays; ?> ];
and it works now...

Related

jquery datepicker disable specific days and dates dynamically

I would like to block specific dates and week days dynamically from MySQL database records. I have done to some extent and not able to proceed further.
json_encode(array("days"=>$days_off,"dates"=>$resultarr))
The above will output the following:
{"days":["0","1","1","1","1","1","0"],"dates":["2015-06-01","2015-06-02","2015-06-03","2015-06-04","2015-06-05"
,"2015-06-06","2015-07-09","2015-07-10","2015-07-28","2015-07-29","2015-07-30","2015-07-31"]}
And in the front end, the jquery written in this way:
success: function(data){
var dateToday = new Date();
var ndata = $.parseJSON(data);
if ($('#datepicker').length) $("#datepicker").datepicker({
minDate: dateToday, firstDay: 1, //endDate: '+0m',
dateFormat: "DD d MM yy", showOtherMonths:false,
beforeShowDay: function(date){
var day = date.getDay();
var selected = $("#datepicker").datepicker('getDate');
return [ndata[day] !=0, ""];
}
});
}
The above will block only the days(monday-sunday) but not able to block specific dates which I get from above json data.
You will need to check if the date is the same as any of the ones passed in your JSON. It could be done like this:
beforeShowDay: function(date) {
//Check if it's a forbidden day of the week.
var day = date.getDay();
var okDay = (ndata.days[day] != 0);
//Check if it's a forbidden date.
var formatted_date = $.datepicker.formatDate('yy-mm-dd', date)
var okDate = ($.inArray(formatted_date, ndata.dates) == -1);
//Only return true if both day of week and date are allowed.
return [okDay && okDate]
}
Please note that I have not tested this code. Also, I changed from ndata[day] to ndata.days[day] since that seemed to be more inline with your JSON, but I might be wrong there. Here is the documentation on .inArray(), and here is the documentation on datepicker.formatDate().

Uncaught ReferenceError: jquery is not defined in Wordpress

Can anyone help me out from this issue. I have added a JS to my WordPress site externally. It doesn't produce the output and throws an error in the following code.
Please help me out and get over it.
var clock;
jquery(document).ready(function() {
var currentDate = new Date();
// Set some date in the past. In this case, it's always been since Jan 1
var pastDate = new Date(currentDate.getFullYear(), - 3, 4, 3);
// Calculate the difference in seconds between the future and current date
var diff = currentDate.getTime() / 1000 - pastDate.getTime() / 1000;
clock = jquery('.clock').FlipClock(diff, {
clockFace: 'MinuteCounter'
});
});
JavaScript is case sensitive. Use jQuery instead of jquery:
jQuery(document).ready(function() {
.
.
.
clock = jQuery('.clock').FlipClock(diff, {
clockFace: 'MinuteCounter'
});
});

MultiDatesPicker addDisabledDates

Error : Uncaught Missing number at position 0
php
$date_disabled = "'03/04/2015','03/10/2015'";
html
<input type="text" id="date" value="<?php echo $date_disabled; ?>" />
javascript
$(document).ready(function(){
$('#booking_date').multiDatesPicker({
addDisabledDates: [$('#date').val()]
});
});
Just wondering if able to pull value from html input text to javascript like this? i'm not sure why is this error appearing...
if i set the value manually into the javascript than the multi date picker is running well without any problem.
$(document).ready(function(){
$('#booking_date').multidatesPicker({
addDisabledDates:['03/04/2015','03/10/2015']
});
});
Thousand appreciate to anyone could help. :D
Use this Demo here
var array = ["14-03-2015","17-03-2015","16-03-2015"]
$('input').datepicker({
beforeShowDay: function(date){
var string = jQuery.datepicker.formatDate('dd-mm-yy', date);
return [ array.indexOf(string) == -1 ]
}
});

JavaScript / PHP: Get a Client's Month and Day with JS, Pass the Values to PHP Variable

I want to get day and month from a client's computer, and then pass it to PHP in two separate variables, which would result in something like this:
$day_num = 11;
$month_num = 12;
I need JavaScript function and PHP function to be separate files, and I need to pass the value
not in the URL.
I would be grateful for a bit of help from an expert!
Javascript:
If you are using jQuery gettting it back to the server is super easy (if you do it without, see: How to make an AJAX call without jQuery?):
<script language="javascript">
var date = new Date(),
month = date.getMonth(),
day = date.getDate();
$.post('path-to-php.php', { day: day, month: month});
</script>
PHP:
<?php
var_dump($_POST['day']);
var_dump($_POST['month']);
?>
Edit:
path-to-php.php is the filename of your php file. It can be absolute or relative, depending on your application. If you wanted to post it to the same file, you could use this instead of path-to-php.php: <?php echo $_SERVER['PHP_SELF'];?>
With javascript:
function executeAjax() {
var date = new Date(),
var month = date.getMonth(),
var day = date.getDate();
var url="daymonth.php?month="+month+"&day="+day;
var objX=new XMLHttpRequest();
objX.open("GET",url,false);
objX.send(null);
var response=objX.responseText;
}
daymonth.php
<?php
$day=$_GET['day'];
$month=$_GET['month'];
?>

The Function listoff is calling always but it should be called only after the timer is expired

The date value is $date = date("D, d M Y H:i:s",1329907734);
$('#expire').countdown({
until: new Date("<?php echo $date; ?>"),
format: 'dHMS',
layout:'{hnn}:'+'{mnn}:'+'{snn}',
onExpiry: liftOff() ,
expiryText:"Expired"
});
The funcion listoff is always calling but it should called only after the timer gets expired
function liftOff()
{
alert('calling');
var reservation_id = $("#reservation_id").val();
window.location = "<?php echo site_url('trips/expire').'/'; ?>"+reservation_id;
}
Assuming that the onExpiry property is expecting a function to run as a callback, you need to omit the parentheses so that you're passing a reference to the liftOff function rather than running the liftOff function:
$('#expire').countdown({
until: new Date("<?php echo $date; ?>"),
format: 'dHMS',
layout:'{hnn}:'+'{mnn}:'+'{snn}',
onExpiry: liftOff,
expiryText:"Expired"
});
You're creating a new Date object for the 'until' init parameter, but setting it's time to "right now". As such liftOff will be called immediately.
Edit: you changed the question's content since I originally answered, it used to be:
until: new Date("")
I bet it's the same issue. Please post the generated HTML source instead of the PHP code, my guess is $date is empty.

Categories