203 Posts
louispapera
10 years ago
14
Topic
Hellò Sebloders, I have two date fields type calendar in a form: Start date and End date.

Now, I  need that the end date must be in the automatic (when I save) = start date + 90 days.

Is this possible ? Many thanks for support. Best regards, Louis
Get a Book for SEBLOD
693 Posts
rpoy
10 years ago
0
Level 1
Hi Louis,

You can do this with the code pack and using the Code-BeforeStore.

Just make sure that when you set the value, you use this format for the End Date calendar: date("Y-m-d H:i:s", $value );

Hope that helps

Randy
10 years ago
2
Level 1
try to set in field settings tab #2: Live value ->Configure->Property->+90
or tab #5 a+b
10 years ago
1
Level 2
I was thinking about the same.

But this will give you always the present day + 90 days.

Louis needs the value of the start date +90 days. Guess, Randy's suggestion would be the way to go.
4229 Posts
Kadministrator
10 years ago
0
Level 3
I tried to do it using computation, but it seems it does not know how to handle dates (unless there is some special syntax for that..) so Randy's suggestion looks like the right way
10 years ago
0
Level 1
There might be a way using Javascript/jQuery in the Stuff->JS area of the start-date field.

You can calculate dates with javascript, see for example here.

Assuming format of date_start field is for example 2014-04-14, you could do something like
$('#start_date').blur(function(){
    startDate = $(this).val().split('-');
    days_to_add = 90;
    $myyear = startDate[0];
    $mymonth = startDate[1];
    $myday = startDate[2];
    date = new Date();
    
    date.setFullYear($myyear,$mymonth,$myday);
    date.setDate(date.getDate()+(days_to_add));
    //console.log(date);
    $('#future_date').val(date.getFullYear()+'-'+date.getMonth()+'-'+(date.getDate()+1));
});
See this working fiddle.

What this code does: when the start_date is filled in and the cursor leaves the input field, the start date + 90days is calculated and filled into the future_date field.

I checked it with this calculator and it seems to be fine.


10 years ago
1
Level 1
Bad news:
I tried to implement this code for testing and it doesn't work in the Seblod context.

Calendar fields are accompanied by hidden fields.

E.g. you have a calendar field named "dat_start". In your form there will be 1 inpout with id="dat_start" and one hidden input with id="dat_start_hidden".
I tried to adjust my jQuery to get the value from $('#dat_start_hidden') and set the value to $('#dat_end_hidden'). But for some reason it doesn't work.
There are no JS conflicts in the console. It seems like the blur event is not being triggered.

I would like to set the date of the end date field to the date of the start date field. So when the user chooses a start date and then goes to select the end date, the calendar widget is set to the start date automatically. But I don't want to hijack this thread. Might open another one for my problem.
10 years ago
0
Level 2
Jquery doesn't work for hidden fields. I have lots of easy solutions with Jquery but I don't know how to make it working with seblod!

As far as I see, Seblod does not render field (what is in "hidden" template position), so Jquery can't move some data to this field because it doesn't see that *** hidden field. Simply. I think because of the security reasons....

Maybe, it's an idea to use some conditional states to set a field as hidden always (let say use any fantastic condition to make it visible)...seems then we will get a parameter type="hidden", where i guess Jquery can copy/calculate some value/text.

Seems topic is related to this http://stackoverflow.com/questions/4376664/jquery-access-input-hidden-value
693 Posts
rpoy
10 years ago
6
Level 1
Hi!

I think the main reason that simply setting the value doesn't work is due to the storage format for the value, and that its JS.  Anything other than the "Y-m-d H:i:s" format didn't work.  This is how to solve the issue:


$value = strtotime($fields['start_date']->value) + (time in seconds);
$newdate = date("Y-m-d H:i:s", $value );

$field = $fields['end_date']->storage_field;
$table = $fields['end_date']->storage_table;

$fields['end_date']->value = $newdate ;
$config['storages'][$table][$field] = $newdate ;

Interesting thing is that the default value can be set with a short format - it would be cool if the default value could be another calendar field.  That would solve your other issue Gerhard.


Randy


10 years ago
4
Level 2
I my form I use two calendar fields. For example publishdate=start of advertising and unpublishdate=publishdate+90 days. 
Use this plugin http://www.seblod.pro/downloads/491-date-math-dlya-seblod-v-3
10 years ago
0
Level 3
Thanks terverg for your solution with that plugin.

Is there an official plugin that we could use instead? Just asking because quite a few of the plugins that are around are outdated. The official ones should be updated frequently once they make it into the shop. And I don't mind paying a few bucks if a plugin is useful.
203 Posts
louispapera
10 years ago
2
Level 3
Dear terverg for your interest in, I installed plugin and try to use it but do not work well for my use.
I need to insert a start date and receive a final date with 90 days later.
Instead, plugin permit me to calculate a final date starting always from date of the day. 
Please, can you help me ?  Regards, Louis
10 years ago
1
Level 4
unfortunately, i'm not so much familiar with PHP and JS (just a bit ...as average user). But I have an idea where to look for.
I guss, if both of your fields are visible, you can try to achieve the goal using Jquery script. What actually you can paste in stuff->js of one of your fields.
For example I found the basic idea here http://stackoverflow.com/questions/16636589/difference-between-2-hours-jquery
you can try to make your fiddle...and if you will success - let us know :)
203 Posts
louispapera
10 years ago
0
Level 5
Dear Terveg, many thanks for your reply, as for you I am also an average user, in any case I will try your suggestions. 
Best regerds, Louis
10 years ago
0
Level 2
Thanks for the code.
it would be cool if the default value could be another calendar field.
I agree. I'll issue a feature request in the tracker.
Get a Book for SEBLOD