Skip Navigation

[Résolu] How to calculate end date, when having a start date and amount of days

This support ticket is created Il y a 1 an et 9 mois. There's a good chance that you are reading advice that it now obsolete.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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)

This topic contains 18 réponses, has 2 voix.

Last updated by fred-r.M Il y a 1 an et 9 mois.

Assigned support staff: Minesh.

Auteur
Publications
#2254511

Tell us what you are trying to do? On this link: hidden link 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?

#2254719

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

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:
=> hidden link

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/

#2255063

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!

#2255121

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:

hidden link

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).

#2255289

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

#2257383

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I've moved the code you added to "Custom Code" section offered by Toolset as well as adjusted the code as given under:
=> hidden link

// 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: hidden link

#2258079

This worked fine, I modified it slightly, but great.

The only thing left, as I do have a view for this link hidden link

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

#2258085

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

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".
- hidden link

#2258155
2022-01-04_16-07-23.jpg

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

#2258209

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

There is a cache issue as well so you should check the page as: hidden link

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.

#2258237

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]
#2258249

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

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:
- hidden link

#2259011

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

You are welcome to mark this ticket resolved.

#2259727

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?

#2259863

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.