Forums

Compare two date

laurentmartin 06 May, 2012
Hello,

I am facing a problem i don't know how to deal properly.

I have two field with date picker on the same form , let's date_1 and date_2

I need to do two comparisons:

Comparison 1 : date_1 - date_2 => so here it will use javascript i assume to compare the value input by customer via the date picker

Comparison 2: date_1 - current_date => here i guess current_date will be given by the php server (i don't want to do it with javascript because it depends on the computer clock)

I would need to trigger some event once the date inside the date picker are selected.

So my questions are as follow:

1./ What event in javascript is triggered by date_picker once the date is selected (i have tried "click" and "change" and so far it doesn't work) ?

2./ How to pass the variable from PHP server to javascript code to perform the calculation inside the function ?

3./ How to get value selected by input in the date picker to put inside the javascript function ?.

Thanks in advanced for your help

PS: i am not a developper :-(
GreyHead 06 May, 2012
Hi laurentmartin,

There are three different Datepickers in the current version of ChronoForms (one in CFv3 and two in CFv4). I suggest that you check the docs for the one uou are using.

If you have CFv4 I recommend that you use the MooTools DatePicker rather than the MonkeyPhysics one.

Bob
laurentmartin 06 May, 2012
I have tried this code but so far it still doesn't work
window.addEvent('domready', function() {
  $('date_1').addEvent(onselect(date), hello);
});

function hello() {
  
  alert ("hello");
});
GreyHead 06 May, 2012
Hi laurentmartin,

Which datepicker are you using?

Bob
laurentmartin 06 May, 2012
Hi Bob,
The one you recommended : MooTools DatePicker
GreyHead 06 May, 2012
Hi laurentmartin ,

Leave just the function in the Load JS action (but remove the extra ) near the end please:
function hello() {
  alert ("hello");
};
and add this to the DateTime Picker config box on the Form Gnereal tab:
onSelect: hello
NB the final , is only needed if you add more than one option in the box.

Bob

PS the config should also work from the Code box of a Custom MooTools Datepicker action (from the Power Fields action group) but I couldn't get it to perform correctly.
laurentmartin 06 May, 2012
Hi bob,
It solves my question 1 indeed.
Any idea for 2./ and 3./ ?
GreyHead 26 May, 2012
Hi laurentmartin,

Just found this thread again - I was away when you last posted I think.

You could use Ajax to get the current time from the server. Here's a basic set of code to do this. I'm not sure that the formatting or the response handling are the best for your need though.

Add this to a Load JS action
window.addEvent('domready', function(){
  var my_time = new Request({
    url: 'http://example.com/index.php?option=com_chronoforms&chronoform=test_form_get_time&event=getTime&format=raw',
    onSuccess: function(responseText){
      $('date_div').set('html', responseText);
    } 
  });
  my_time.send();
});


Add this to to a new getTime event:
<?php
echo date('Y-m-d H:i:s');
$mainframe =& JFactory::getApplication();
$mainframe->close();
?>


This puts the current server time into a div with the id date_div (you need to create the div) when the page loads. You can change the event to tie it to a date selector onSelect event if you need more precision.

Bob
laurentmartin 26 May, 2012
Hi bob,

Thanks for coming back on this.

I will give a try to this.

REgards
This topic is locked and no more replies can be posted.