1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

Date element (advanced)

Discussion in 'Community' started by RobertG, Apr 9, 2020.

  1. RobertG

    RobertG Member

    Level: Community
    Hi,

    I'm trying to calculate dates from "today" and read the wiki page for date element but can't understand how to calculate.

    I tried with element date and jdate (actually jdate).

    On a jdate field, I would like to display the next sunday as default value. On another the next saturday after this sunday. The visitor can choose the first date from tomorrow to next sunday, the second from the following saturday.
    It's a stays choice. The stays begin on sunday and ends on saturday. The student can book from before sunday and leave after saturday, so I will need to calculate additional nigths to.

    For the moment, I am trying to set tomorrow as default value, but don't find what to add in the default value parameters.
    Eval is set to "Yes", default value to "date('d') + 1)" (without quotes), but I have an error in the form: "DateTime::__construct(): Failed to parse time string (date('d') + 1)) at position 0 (d): The timezone could not be found in the database".
    What is my mistake?

    For validation of the first date choice, I accept only dates from tomorrow with javascript, it works.
    I would like to display only date from tomorrow in the calendar too: is it possible?

    Thanks for you help
     
  2. troester

    troester Well-Known Member Staff Member

    Level: Community
  3. RobertG

    RobertG Member

    Level: Community
    Thanks troetser! I viewed the two first pages and knowed the third, not the last.
    I forgot to use "return".

    I succeeded to set the default dates:
    and
    I don't find how to calculate the second date when the first change.
    In the php documentation for relative dates, a sample is "date('F jS, Y', strtotime('next Monday 2012-04-01'));" to find a date from 2012-04-01
    I tried "return date('Y-m-d',strtotime('saturday next week ' ,strtotime( nmne2_fb_stay.arrival_date')));" but the element jdate displays "1970-01-01".

    I need also to compare selected first date with the first sunday (the same date or some days later) and the last date with the last saturday to know if there are additional days but for the moment, I don't know how to add the selected date to the strtotime function in these two situations too.[/QUOTE][/QUOTE]
     
    Last edited: Apr 9, 2020
  4. RobertG

    RobertG Member

    Level: Community
    I forgot: the form use Ajax.
     
  5. RobertG

    RobertG Member

    Level: Community
    I'm trying to allow dates (from PHP allow dates in Wiki sample page) and have an error I don't understand.
    I went back to element date instead of jdate to use advanced tab a insert sample code wit my own parameters (table and field).
    The code
    $myDb = JFactory::getDbo();
    $myQuery = $myDb->getQuery(true);
    This first line is correct but create an error, why?
    upload_2020-4-9_18-41-6.png
    Despite the error, the allowed dates are set, but I can't open the calendar.
    I changed to allow all dates from now, without looking to the table, but have the same error for
    $interval = DateInterval::createFromDateString('1 day');
     
    Last edited: Apr 9, 2020
  6. troester

    troester Well-Known Member Staff Member

    Level: Community
    I assume you put this code into the "Allow date" field. This field is for JS.
    php code goes into "PHP allow date"
     
  7. RobertG

    RobertG Member

    Level: Community
    No, in the advanced tab of element date:
    upload_2020-4-9_19-15-35.png
    $thisDate is displayed with "echo", but I see only the date field, and can't open the date picker, the icons are inactive.
    upload_2020-4-9_19-19-37.png
    How to use $myAllowedDates in the datepicker?
     
  8. RobertG

    RobertG Member

    Level: Community
    I change to use in the JS Allow date field this sample code
    var diff = new Date().compare(new Date(date));
    var result = diff < 1 ?true : false;
    and I have a JS error
    upload_2020-4-9_19-30-39.png

    Sorry! It remained an element jdate! No more JS error, the buttons open the date pickers.
     
    Last edited: Apr 9, 2020
  9. RobertG

    RobertG Member

    Level: Community
    Sorry, I am trying to calculate from the arrival date element selected date the new date in the departure date element and not allow the dates before the first saturday from the arrival date but I don't find how to obtain the value of the arrival date element, in php and in JS.
    I don't want to get the value from database but the actual value of the form field.

    I tried (sample from http://fabrikar.com/forums/index.php?wiki/element-javascript-advanced/) in the advanced tab, JS field allow date function
    var arr = Fabrik.getBlock('form 17').formElements.get('nmne2_fb_stay___arrival_date');
    but there is a JS error and the datepicker is not displayed. (the form name is "stay", its id is 17, "arrival_date" is the name of the field)

    Please help me !
     
    Last edited: Apr 10, 2020
  10. RobertG

    RobertG Member

    Level: Community
    2020-04-12 is selected in arrival date.
    I tried to use {nmne2_fb_stay___arrival_date} in default field (options) and in PHP Allow date function: always empty.
    If I use
    $app = JFactory::getApplication();
    $f = $app->input->get('nmne2_fb_stay___arrival_date', 'default value', 'string'); //'string' = filter e.g. 'INT','STRING'
    $f return 'default value', not a value.

    I tried too
    $app = JFactory::getApplication();
    $f = $app->input->getInt('nmne2_fb_stay___arrival_date');
    echo (int)$f;
    result = '0' (zero)
     
    Last edited: Apr 11, 2020
  11. RobertG

    RobertG Member

    Level: Community
    Hi,

    I tried $POST and $_GET but always got "empty"
    Code (Text):
    $arr_date = $_POST['nmne2_fb_stay___arrival_date'];
    if (isset($arr_date)) {
      print_r ($arr_date);
    } else {
      print_r ('empty');
    }
     
  12. troester

    troester Well-Known Member Staff Member

    Level: Community
    Not sure what you are trying to do.
    The "(php) allowed date"s are for creating the date picker.
    If you want to handle dates "during" the input you must use the element or form JS (POST and GET is communication with the server, e.g. on page load, and there are Joomla functions to access the params ).
     
  13. RobertG

    RobertG Member

    Level: Community
    Thanks!

    With JS, it works, but how to do with PHP?
    Code (Text):
    var name = Fabrik.getBlock('form_17').elements.get('nmne2_fb_stay___arrival_date');
    value = name.get('value');
    alert(value);
    alert shows the selected date.

    I am not able to calculate with JS, but can try with PHP.

    Today the arrival date can be selected from tomorrow to a future sunday, as the stage begins on sunday and ends on next saturday. By default, arrival date is set to next sunday but can be selected many weeks later.
    So I need to calculate the default departure day not from today but from the selected date and allow to select departure date only from the default date.

    Actually, departure date is calculated from today and is wrong with my previous PHP code (the next saturday is today saturday 2020 april 18, not 25 because the next stage will begin on 2020-04-19), so I need to change this code
    Code (Text):
    date_default_timezone_set('UTC');
    return date('Y-m-d',strtotime('saturday next week'));
    For example, I select the arrival date 2020-06-21 and want to automatically change the departure default date to 2020-06-27 and allow only dates from 2020-06-27 for departures.
     
  14. troester

    troester Well-Known Member Staff Member

    Level: Community
    PHP is running on the server, you have to use JS.
     
  15. RobertG

    RobertG Member

    Level: Community
    So I will have to give up! :(

    Thanks troester!
     
  16. troester

    troester Well-Known Member Staff Member

    Level: Community
  17. RobertG

    RobertG Member

    Level: Community
    Thanks, I will try.
    But I don't understand how to define default value in the options tabs. With JS and Eval to "no"?
     
  18. RobertG

    RobertG Member

    Level: Community
    Hi,

    I set the default values with PHP and advanced code with JS.

    I can now change the departure date when the arrival date is changed (the allowed dates change in the departure datepicker), but the displayed value of the departure field is not modified. If I open the departure datepicker, this date is now correctly displayed.

    For example: initial default values are 2020-04-19 for arrival, 2020-04-25 for departure. I select 2020-04-26 for a new arrival, the new departure date is calculated to 2020-05-02, previous dates are deactivated in the calendar but the date element field is not refreshed.
    I open the page
    upload_2020-4-14_17-41-6.png
    I change the arrival
    upload_2020-4-14_17-42-53.png
    I simply open the calendar, the right date is displayed
    upload_2020-4-14_17-43-50.png

    How can I refresh without clicking on the calendar icon?
    Thanks
     

    Attached Files:

  19. troester

    troester Well-Known Member Staff Member

    Level: Community
  20. RobertG

    RobertG Member

    Level: Community
    Thanks troester. I will try another time (I did it many times with errors).

    I am using this code but alert is not displayed. :(
    Code (Text):

    var to_date_id = 'nmne2_fb_stay___departure_date';
    var to_date = Fabrik.getBlock('form_17').formElements.get(to_date_id);
    var from_date_id = 'nmne2_fb_stay___arrival_date';
    var from_date = Fabrik.getBlock('form_17').formElements.get(from_date_id);
    var fromDate = this.get('value');
    var refDate = new Date(fromDate); // date d'arrivée sélectionnée
    alert(refDate);
     
    What is my mistake?
     

Share This Page