Accueil › Toolset Professional Support › [Résolu] How to calculate end date, when having a start date and amount of days
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | - |
- | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Supporter timezone: Asia/Kolkata (GMT+05:30)
Ce sujet contient 18 réponses, a 2 voix.
Dernière mise à jour par fred-r.M Il y a 2 années et 11 mois.
Assisté par: Minesh.
Tell us what you are trying to do? On this link: lien caché we do have a "start date". We do have a field tour-days (Tour Days, number) where we have the amount of days.
In a view I am using on the link above, I do show actually the start date. Now I would like to add the end date. Is there a possibility to calculate it?
Is there any documentation that you are following? I did not found anything
Is there a similar example that we can see?
What is the link to your site?
Hello. Thank you for contacting the Toolset support.
Yes, its possible to add number of days to your existing start date field and display the calculated result (start date + number of days) as end date but for that you will have to write custom shortcode where you should pass current start date and number of days and return the calculated result.
More info:
=> lien caché
Here is the Doc about how you can create custom shortcode:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/how-to-create-a-custom-shortcode/
Dear Minesh
I will close so far this ticket, whenever I have an issue with creating it, I will get back to this ticket.
My issue is resolved now. Thank you!
Dear Minesh
I do have basically this:
// End date calculation function custom_tourend_date( $atts ) { $adate = ''; $adays = ''; // Attributes $atts = shortcode_atts( array( 'tourdate' => 'today', 'days' => '1', ), $atts ); $adate = $atts['tourdate']; $adays = $atts['days']; if(ICL_LANGUAGE_CODE=='en') {$enddate = date('l, d. M Y', strtotime($adate. ' + '.$adays.' days'));} elseif (ICL_LANGUAGE_CODE=='de') {setlocale(LC_ALL, 'de_DE'); $enddate = strftime(date('l, d. M Y', strtotime($adate. ' + '.$adays.' days')));} return $enddate; } add_shortcode( 'tourenddate', 'custom_tourend_date' );
and in the view, I do have this short code:
[tourenddate tourdate='[types field='tour-date-field' style='text' format='Y-m-d'][/types]' days='[types field='tour-days' format='FIELD_VALUE' item='@tourid.parent'][/types]']
This works fine in English, but in the German version, I don't get the day in the correct language. You can see the result here:
lien caché
The part is where You see the yellow button (this is the normal start date, done with toolset short code field. The green button with my shortcode. Do You have a tip how can I bring the day respectively the date into a correct language (german).
I'm not sure whats going wrong with your setup.
I'll have to check and for that I'll require admin access details and please tell me where you added the code you shared.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Can you please check now: lien caché
I've moved the code you added to "Custom Code" section offered by Toolset as well as adjusted the code as given under:
=> lien caché
// End date calculation function custom_tourend_date( $atts ) { $adate = ''; $adays = ''; // Attributes $atts = shortcode_atts( array( 'tourdate' => 'today', 'days' => '1', ), $atts ); $adate = $atts['tourdate']; $adays = $atts['days']; if(ICL_LANGUAGE_CODE=='en') {$enddate = date('l, d. M Y', strtotime($adate. ' + '.$adays.' days')); } elseif (ICL_LANGUAGE_CODE=='de') { setlocale(LC_ALL, 'de_DE.UTF-8'); $enddate = strftime('%A %e %B %Y', strtotime($adate. ' + '.$adays.' days')); } return $enddate; } add_shortcode( 'tourenddate', 'custom_tourend_date' );
I've changed the following line of code from:
$enddate = strftime(date('l, d. M Y', strtotime($adate. ' + '.$adays.' days')));
To:
$enddate = strftime('%A %e %B %Y', strtotime($adate. ' + '.$adays.' days'));
Can you please confirm it works as expected: lien caché
This worked fine, I modified it slightly, but great.
The only thing left, as I do have a view for this link lien caché
And the "start" date I get like this:
[types field='tour-date-field' style='text' format='d. M Y'][/types]
It shows me for the month in German "Mrz" - in the code above I do get "Mär". Both are correct, but I like to have just one version. Any idea how to solve this? Possible to change the short code I mentioned slightly?
Regards,
Simon
I've displayed the start date using the custom short code as given under:
[tourenddate tourdate="[types field='tour-date-field' style='text' format='Y-m-d'][/types]" type="date"]
I can see its working as expected and displays "Mär" instead of "Mrz".
- lien caché
Well, I do see 2 different "format" showing, Mrz and Mär. This only when I am logged out. When I am logged in, then I see both "buttons" showing Mär.
In the end, I don't care what version we would have, either Mär or Mrz is fine for me. Mrz used to be better.
Regards,
Simon
There is a cache issue as well so you should check the page as: lien caché
And I've replaced the "Mär" with "Mrz" using the following line of code that is added to custom code section:
$enddate = str_replace("Mär","Mrz",$enddate);
I can see now it displays Mrz as month name.
Thanks, what if the client like to have "Mär" instead of "Mrz"? Are I am able to change it in the Toolset short code?
[types field='tour-date-field' style='text' format='d. M Y'][/types]
No, you should change the following line of code frm
$enddate = str_replace("Mär","Mrz",$enddate);
To:
$enddate = str_replace("Mrz","Mär",$enddate);
In the Custom shorcode we have added:
- lien caché
You are welcome to mark this ticket resolved.
Dear Minesh
I understand for the custom coding part. But for the part in the view - where we do have the Toolset Short code:
[types field='tour-date-field' style='text' format='d. M Y'][/types]
- is there a way to show "Mär" instead of "Mrz" - in the German version?
You do not have to change anything with the shortcode.
As I shared with my pervious reply:
https://toolset.com/forums/topic/how-to-calculate-end-date-when-having-a-start-date-and-amount-of-days/#post-2258249
You need to make change there.
If you want to show "Mär" then you should find the following line of the code:
$enddate = str_replace("Mrz","Mär",$enddate);
You can change the replace the word as per your requirement. Either Mrz to Mär or Mär to Mrz.