How to save calc plugin element as date, not text

Hi all

Grateful for any assistance for this struggling but enthusiastic newbie.

I am trying to create a membership app. I have an element "season start date" (jdate plugin) which the user completes manually. Then I have an "season end date" element to calculate 364 days ahead. I have achieved this using a calc plugin and the code:
$season_end_date = date('jS F, Y', strtotime('{maintenance___season_start_date_raw}' . " +1 year" . " -1 day"));
return $season_end_date;

This works ok but I then need to use this element in other lists as a date and the calc element is storing this as text. I'm assuming this is because I'm using a string variable in the calculation but I've tried other ways and can't get it to work.

I've also tried a "season end date" element as a jdate plugin using a similar calculation but without using a string variable. The best result I can get is the date displaying as 1st January 1970. Clearly I'm using the wrong code but my php skills are severely limited and I can't work out what I should be doing.

Any ideas please?

Thanks,
Steve
 
Couple of options:

1) Have two calc elements where the first one stores the end date as Y-m-d h:i:s and then use another calc element for formatting and displaying the date.
2) Or in your other table/element format the jS F, Y date as Y-m-d h:i:s before making any calculations with the date.
 
Thank you for that, @juuser

I tried option one, changing the end date calc plugin element code to:
$season_end_date = date('Y-m-d h:i:s', strtotime('{maintenance___season_start_date_raw}' . " +1 year" . " -1 day"));
return $season_end_date;

This seems to work and, using the autofill form plugin, I can put this into another list where it is formatted as VARCHAR instead of TEXT in the database. I now need to check whether I can do any calculations on this (autofilled) field, such as whether this date is greater or less than today. (More research needed!)

At this stage I'm not sure whether I need to display the end date as jS F, Y but if I do, I'll try your option 1, part 2 solution.

Thanks again.
 
another approach would be to have two date element and use some js code (not the best one ! :) :
onChange of the first date element add this code :
JavaScript:
/* déclenche date de fin = date de début + 1 an */
var form = Fabrik.getBlock('form_12');
var debut = form.formElements.get('contrat___cntr_date_debut').get('value');
var fin   = form.formElements.get('contrat___cntr_date_fin');
var newDate = new Date(debut);
newDate.setYear(newDate.getFullYear() + 1);
fin.update(newDate.format('%Y-%m-%d %H:%i:%s'));
Of course change with your own values :
form_12 where "12" is your form id,
contrat___cntr_date_debut is the full name of the first date (the one on which there is the actual js code)
contrat___cntr_date_fin is the full name of the second date element.
"debut" means "start" in french and "fin" means "end"

You can apply the format of your convenience on the date elements.
 
Thank you, Laurent

I created two new elements using jdate plugin, copied and pasted the code into the start date element under the Javascript tab and set the Event as "change". Then I changed all the relevant values as you said:
var form = Fabrik.getBlock('form_6');
var start = form.formElements.get('maintenance___season_start_date_js').get('value');
var end = form.formElements.get('maintenance___season_end_date_js');
var newDate = new Date(start);
newDate.setYear(newDate.getFullYear() + 1);
end.update(newDate.format('%Y-%m-%d %H:%i:%s'));

But for some reason, when I enter a date into the start date element, nothing is shown in the end date element. Have I missed something?

Thanks,
Steve
 
Actually, I'm using regular date element. Not the jDate one.
You'll have to switch all the date element of your form to the regular "date" one or... have someone who could help on this one.
Sorry
 
Another approach:
A calc element is default stored as 'text', but that does not mean you cannot change that.
I often use calc elements to store decimal or int. Didn't try date yet, but that should work also.
If you have access to the database, you can use phpmyadmin to change the type in your database into 'date' after you created the calc element.
Of course then you have to use the database format for the calc element and put it in quotes:
return "'".date('Y-m-d', strtotime('{maintenance___season_start_date_raw}' . " +1 year" . " -1 day"))."'";
 
Thanks for that, @henk
It worked perfectly!
I can use the calculated element to autofill an element on another form (as a VARCHAR field). I now need to check whether I can use that element to compare to today's date. I'm hoping that will work.
(Sorry for the late response, I had to shelve the project for a week.)
Steve
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top