Skip Navigation

[Résolu] How to get the values of a date field?

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I would like to get the values of a repeating date custom field, and output a custom data structure including the formatted dates.

Solution:
Custom code is required here. In general, you can get the value of a custom field using the WordPress API get_post_meta. If the Types start date field slug is 'start-tour-date', then the get_post_meta function will look something like this, using a wpcf- prefix for the field slug:

$post_id = 1234;
$start_dates = get_post_meta($post_id, 'wpcf-start-tour-date', false);

Note that the Types field slug includes a wpcf- prefix here. This will return an array of date values in Unix timestamp format.

To get a single field value, like the number of tour nights, you can use get_post_meta again like so:

$number_of_tour_nights = get_post_meta($post_id, 'wpcf-number-of-tour-nights, true);

Once you have those values, you can loop over the repeating values and create your own array of information to output. You'll need a way to format dates based on Unix timestamps. You can use PHP's date function for this. For example, to format a date like 2021-08-31 from a Unix timestamp like 1630368000, you can use the date function like so:

$timestamp = 1630368000;
$formatted_date = date('Y-m-d', $timestamp);

If you want to calculate a date in the future based on the start date and number of nights, you can use the number of seconds in a day (60 seconds per minute * 60 minutes per hour * 24 hours per day) in your PHP calculations, then format the end date:

$start_date = 1630368000;
$number_of_nights = 4;
$end_date = $start_date + (60 * 60 * 24 * $number_of_nights);
$formatted_end_date = date( 'Y-m-d', $end_date);

Relevant Documentation:
https://developer.wordpress.org/reference/functions/get_post_meta/
https://www.php.net/manual/en/function.date.php
https://toolset.com/documentation/customizing-sites-using-php/functions/

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.

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

Last updated by fred-r.M Il y a 6 mois et 1 semaine.

Assigned support staff: Christian Cox.

Auteur
Publications
#1906047

Tell us what you are trying to do?

I like to get all values from a date field (I know there is some coding included) to get in the end this result:

[
{start: '2021-08-31', end: '2021-09-05'},
    {start: '2021-09-11', end: '2021-09-15'},
    {start: '2021-09-15', end: '2021-09-23'},
    {start: '2021-10-01', end: '2021-10-07'}
]

The date field is define like this:

Field Type: Date (Input only the date)
Single or repeating field? Allow multiple instances of this field

I use this field on a single product (WooCommerce) and the results will be in a calendar (provided some code from myself) where I can use the "form" above.

Additional field we like to use is the amount of days - so the end date will be 'start-tour-date' + 'number-of-tour-nights'

I know - respectively I am sure there is an easy way to get all information (values) from each start-tour-date.

Is there any documentation that you are following? I didn't found any documentation.

What I expect, if You could help with a basic coding (php), to get it done, having it as a variable or so is fine.

Is there a similar example that we can see?

In the end I like to have this:

[
{start: '2021-08-31', end: '2021-09-05'},
    {start: '2021-09-11', end: '2021-09-15'},
    {start: '2021-09-15', end: '2021-09-23'},
    {start: '2021-10-01', end: '2021-10-07'}
]

What is the link to your site? hidden link

#1906743

Christian Cox
Supporter

Languages: Anglais (English )

Timezone: America/New_York (GMT-04:00)

Hello, it sounds like you need assistance outputting a custom data structure given some Types field values. Most of this custom code is your responsibility, as it falls outside the scope of support we offer here: https://toolset.com/toolset-support-policy/.

However, I can share some information you might find helpful in crafting a custom code solution. In general, you can get the value of a custom field using the WordPress API get_post_meta:
https://developer.wordpress.org/reference/functions/get_post_meta/
If the Types start date field slug is 'start-tour-date', then the get_post_meta function will look something like this, using a wpcf- prefix for the field slug:

$post_id = 1234;
$start_dates = get_post_meta($post_id, 'wpcf-start-tour-date', false);

Note that the Types field slug includes a wpcf- prefix here. This will return an array of date values in Unix timestamp format.

I assume the number of tour nights field only allows one value, it is not repeating. To get a single field value, like the number of tour nights, you can use get_post_meta again like so:

$number_of_tour_nights = get_post_meta($post_id, 'wpcf-number-of-tour-nights, true);

Once you have those values, you can loop over the repeating values and create your own array of information to output. You'll need a way to format dates based on Unix timestamps. You can use PHP's date function for this: https://www.php.net/manual/en/function.date.php

For example, to format a date like 2021-08-31 from a Unix timestamp like 1630368000, you can use the date function like so:

$timestamp = 1630368000;
$formatted_date = date('Y-m-d', $timestamp);

If you want to calculate a date in the future based on the start date and number of nights, you can use the number of seconds in a day (60 seconds per minute * 60 minutes per hour * 24 hours per day) in your PHP calculations, then format the end date:

$start_date = 1630368000;
$number_of_nights = 4;
$end_date = $start_date + (60 * 60 * 24 * $number_of_nights);
$formatted_end_date = date( 'Y-m-d', $end_date);

I hope this helps. Let me know if you need additional help getting the values of a Types custom date field, and I can give more assistance on that topic. If you need additional information about creating a custom output structure, you may need the assistance of an independent contractor.

#1907387

Dear Christian Cox

Thank You for this part of coding...

Now I need to create a loop... basically with those information and pieces of code:

If You could help me with it would be great...

I need to have in the end this together:

"09/01/2021","20/02/2021","03/04/2021"

And here is some coding from You, and what I have already, from /* shortcode to st datepicker gravity forms */. But I struggle to add the foreach loop. I am sure the dates I have are in an array, as It should show all dates used in this post (Woocommerce product).

$start_date = 1630368000;
$number_of_nights = 4;
$end_date = $start_date + (60 * 60 * 24 * $number_of_nights);
$formatted_end_date = date( 'Y-m-d', $end_date);

$tour_date = get_post_meta($post->ID, 'wpcf-start-tour-date', false);
$number_of_tour_nights = get_post_meta($post_id, 'wpcf-number-of-tour-nights, true);

$tourdates = array("red", "green", "blue", "yellow");

foreach ($tourdates as $value) {
  echo "$value<br>";
}

/* shortcode to st datepicker gravity forms */
function display_gf_datepicker() {
  
  global $post;
  $number_of_tour_nights = get_post_meta($post->ID, 'wpcf-number-of-tour-nights, true);
  $str = '';

  $str .= '<script type="text/javascript">';
  $str .= 'gform.addFilter( "gform_datepicker_options_pre_init", function( optionsObj, formId, fieldId ) {';
  $str .= 'if ( formId == 3 && fieldId == 8 ) {';
  $str .= 'var enableDays = ["09/01/2021","20/02/2021","03/04/2021"];';
  $str .= 'optionsObj.beforeShowDay = function(date) {';
  $str .= 'var checkdate = jQuery.datepicker.formatDate("dd/mm/yy", date);';
  $str .= 'return [enableDays.indexOf(checkdate) != -1];';
  $str .= '};';
  $str .= '}';
  $str .= 'return optionsObj;';
  $str .= '});';
  $str .= '</script>';
	
  return $str;
}

add_shortcode( 'show_gf_datepicker', 'display_gf_datepicker' );
#1907477

Dear Christian Cox

I am having this code. As I am not a "big" php-programmer, I appreciate if You can check it and give some feeback. It's working - so far nice You find it here in the bottom part where I have a test form from Gravity:

hidden link

To mention it's the tour date to choose.

/* shortcode to st datepicker gravity forms */
function display_gf_datepicker() {
  
  global $post;
  $str = '';

  $str .= '<script type="text/javascript">';
  $str .= 'gform.addFilter( "gform_datepicker_options_pre_init", function( optionsObj, formId, fieldId ) {';
  $str .= 'if ( formId == 3 && fieldId == 8 ) {';
  $str .= 'var enableDays = ['.get_my_dates().'];';
  $str .= 'optionsObj.beforeShowDay = function(date) {';
  $str .= 'var checkdate = jQuery.datepicker.formatDate("dd/mm/yy", date);';
  $str .= 'return [enableDays.indexOf(checkdate) != -1];';
  $str .= '};';
  $str .= '}';
  $str .= 'return optionsObj;';
  $str .= '});';
  $str .= '</script>';
	
  return $str;
}
add_shortcode( 'show_gf_datepicker', 'display_gf_datepicker' );

function get_my_dates() {
  
  global $post;
  $str = '';
  $tourdates = get_post_meta($post->ID, 'wpcf-start-tour-date', false);
  $alltourdates = count($tourdates);
  $mycounter = 0;
  while ($mycounter < $alltourdates)
  {
	  $myresults = date( 'd/m/Y', $tourdates[$mycounter]);
	  $mycounter++;
	  /*$str .= $mycounter;*/
	  $str .= '"'.$myresults.'"';
	  $str .= ',';
  }
  return $str;
}
#1908121

Christian Cox
Supporter

Languages: Anglais (English )

Timezone: America/New_York (GMT-04:00)

As I am not a "big" php-programmer, I appreciate if You can check it and give some feeback.
Hi, it looks like you were able to solve this with a "while" loop. Is there something specific you need from Toolset at this point, or are you looking for general feedback? In general, I think your solution here is a good one. I am able to see you have used the get_post_meta function to get information from a Types field here:

$tourdates = get_post_meta($post->ID, 'wpcf-start-tour-date', false);

That looks great. You are counting the number of dates using the PHP count function - that seems fine as well. Then you have used a "while" loop to loop over those dates and activate each specific date in a gform calendar widget by writing to a string of JavaScript configurations:

$myresults = date( 'd/m/Y', $tourdates[$mycounter]);

It seems to work well when there are multiple tour dates. I suggest testing these specific scenarios to confirm the code is solid:
- What happens if a post has no tour dates? Does the PHP code or JS code throw errors?
- What happens if a post has only one tour date? Does the PHP code or JS code throw errors?
- What happens if the start dates are not in chronological order, like if the dates are 1610880000, 1610890000, 1610870000, does the calendar work as expected?

#1909965

My issue is resolved now. Thank you!