Home › Toolset Professional Support › [Resolved] Calculate a new date and store it in a custom field
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 |
---|---|---|---|---|---|---|
- | 9:00 – 12:00 | 9:00 – 12:00 | 9:00 – 12:00 | 9:00 – 12:00 | 9:00 – 12:00 | - |
- | 13:00 – 18:00 | 13:00 – 18:00 | 13:00 – 18:00 | 14:00 – 18:00 | 13:00 – 18:00 | - |
Supporter timezone: America/Jamaica (GMT-05:00)
Related documentation:
This topic contains 11 replies, has 3 voices.
Last updated by Rune Brynestad 4 years, 8 months ago.
Assisted by: Shane.
I need help with a function. I have two fields in my Toolset form. ['start_date'] that comes from a datepicker custom field in the form and ['year-to-publish'] that is an integer value from a dropdown menu in the form.
From these two fields I need to calculate a publishing date ['start_date'] + ['year-to-publish']
I've started writing the function, but need help calculating the publishing date the number of years ahead from the start date.
Thanks in advice.
Kind regards
Rune
//Calculate a publishing date from a starting date . add_action('cred_save_data', 'calculate_publishing_date',10,2); function calculate_publishing_date($post_id, $form_data) { if ($form_data["id"]==3260) { if(isset($_POST['start_date'])){ // ['start_date'] comes from a datepicker custom field in the form $start_date = new DateTime(date('Y-m-d', $_POST['start_date'])); } if(isset($_POST['year-to-publish'])){ // ['year-to-publish'] comes from a dropdown menu custom field in the form $number_of_years = $_POST['year-to-publish']; } $publishing_date = $start_date + $number_of_years * 365 * 24 * 60 * 60; // calculate the publishing date (here I'm on thin ice) update_post_meta($post_id, 'wpcf-publishing-date', $publishing_date); // 'wpcf-publishing-date' is the new date to store in a custom field. } }
Hello, I have a few bits of advice for you. First and most important is to use the PHP function error_log. If you're not familiar with error_log, take a few minutes to research it and it will save you hours of time. You can use the error_log function to write out to a server log file as code is executing, and it's an invaluable tool for PHP development. You can write out a simple string or value directly using
error_log('string');
...if the data you want to log is an array or object $foo, you should use print_r with error_log like so:
error_log(print_r($foo, true));
If you're not familiar with turning on server logs, I can show you how to turn them on temporarily in most common wp-config.php setups. Go in your wp-config.php file and look for
define('WP_DEBUG', false);
Change it to:
define('WP_DEBUG', true);
Then add these lines, just after the WP_DEBUG line:
define('WP_DEBUG_LOG', dirname(__FILE__) . '/error_log.txt'); define( 'WP_DEBUG_DISPLAY', false ); @ini_set( 'display_errors', 0 ); define('WP_DISABLE_FATAL_ERROR_HANDLER',true);
Update wp-config.php on your server. Now you can use the error_log function to create and write to an error_log.txt file in the root directory of your site - the same directory that holds wp-config.php in most setups. Once you have that log up and running, use error_log() in your code to write to the logs as code is executed. When you're done, revert the changes you made in wp-config.php and you can delete the log file. I've added two error_log statements to your code below, which should help you see what's going on and where the problems lie:
//Calculate a publishing date from a starting date . add_action('cred_save_data', 'calculate_publishing_date',10,2); function calculate_publishing_date($post_id, $form_data) { if ($form_data["id"]==3260) { error_log('Form 3260 has been submitted. The POST superglobal contains: '); error_log(print_r($_POST, true)); if(isset($_POST['start_date'])){ // ['start_date'] comes from a datepicker custom field in the form $start_date = new DateTime(date('Y-m-d', $_POST['start_date'])); } if(isset($_POST['year-to-publish'])){ // ['year-to-publish'] comes from a dropdown menu custom field in the form $number_of_years = $_POST['year-to-publish']; } $publishing_date = $start_date + $number_of_years * 365 * 24 * 60 * 60; // calculate the publishing date (here I'm on thin ice) update_post_meta($post_id, 'wpcf-publishing-date', $publishing_date); // 'wpcf-publishing-date' is the new date to store in a custom field. } }
The two log statements should tell you when this Form has been submitted, and then give you a breakdown of all the information captured in the $_POST superglobal. Beyond that, some additional advice specific to your code here:
1. A Types custom datepicker field will use the 'wpcf-' prefix. So if the field slug in wp-admin is start_date, the field key in $_POST will be wpcf-start_date.
2. A Types datepicker field will be passed in the $_POST superglobal as an array of information, not a single value. To access the Unix timestamp of the datepicker field selection, you can do something like this in PHP:
$timestamp = $_POST['wpcf-start_date']['timestamp'];
3. Check out this stackoverflow post that discusses using strtotime to add time to a timestamp:
https://stackoverflow.com/questions/5172849/php-adding-years-to-a-timestamp
$newTimestamp = strtotime('+1 year', $timestamp);
No need for using DateTimes or any other conversions here, just timestamp manipulation.
Let me know if you get stuck.
Hi Christian
Thanks for looking into this. I got it to work in a way.
$newTimestamp = strtotime('+1 year', $timestamp);
add 1 year and update the 'wpcf-publishing-date' field with a date 1 year from start_date
$newTimestamp = strtotime('+1 year', $timestamp);
add 2 years and so on
But the number of years to add comes from the variable $number_of_years which in turn gets its value from the dropdown menu with the slug 'year-to-publish', so I change the code to
$newTimestamp = strtotime('+$number_of_years year', $timestamp);
That did not work. Is it something wrong with the syntax in that line of code?
Thanks again.
Kind regards
Rune
Hi Rune,
Christian is currently unavailable today but he will be back tomorrow to continue assisting with this.
Thanks,
Shane
Any progress on this?
Kind regards
Rune
Hi Rune,
Christian is still not available, however can you try this below.
$newTimestamp = strtotime('+'.$number_of_years.' year', $timestamp);
What I'm essentially doing is concatenating the variable to the statement so that the variable can get rendered.
Please let me know if this works.
Thanks,
Shane
Unfortunately, it did not work
Here is how my function looks like now:
add_action('cred_save_data', 'calculate_publishing_date',10,2); function calculate_publishing_date($post_id, $form_data) { if ($form_data["id"]==3260) { error_log('Form 3260 has been submitted. The POST superglobal contains: '); error_log(print_r($_POST, true)); if(isset($_POST['wpcf-dodsdato']['timestamp'])){ $timestamp = $_POST['wpcf-dodsdato']['timestamp']; } if(isset($_POST['tidspunkt-for-publisering'])){ $number_of_years = $_POST['tidspunkt-for-publisering']; } $newTimestamp = strtotime('+'.$number_of_years.' year', $timestamp); // $newTimestamp = strtotime('+4 year', $timestamp); (Testing without using $number_of_years works) update_post_meta($post_id, 'wpcf-publiseringsdato', $newTimestamp); } }
When I use the PHP function error_log I can see that thre is an Undefined index: tidspunkt-for-publisering
I'm attaching the error log disguised as a jpg. Please rename the field to .txt after download
Also attaching the custom field setup for the tidspunkt-for-publisering which is a dropdown with numeric values indicating the numbers of year to add
Finally the actual cred group from the user form
[cred_show_group if="( $(hvem-skal-minnesiden-handle-om) eq '2' ) AND ( '[wpv-post-author format='meta' meta='ID']' eq '[wpv-user field='ID']' )" mode="none"] <div class="form-group"> <label>Fremtidig innlegg</label> [cred_field field="innhold" force_type="field" class="form-control" output="bootstrap"] </div> <div class="form-group"> <label>Tidspunkt for publisering. Velg antall år fra dødsdato</label> [cred_field field="tidspunkt-for-publisering" force_type="field" class="form-control" output="bootstrap"] </div> <div class="form-group"> [cred_field field="bekreft-ok" force_type="field" class="form-control" output="bootstrap"] </div> [/cred_show_group]
Thanks for looking into this.
Kind regards
Rune
Hi Rune,
I tested this code line here:
$newTimestamp = strtotime('+'.$number_of_years.' year', $timestamp);
It works fine, the number of years gets returned perfectly fine as a timestamp.
Are you experiencing any errors ? Does the $newTimestamp variable return any value ?
Add this to your code to see what gets returned.
die($newTimestamp);
Thanks,
Shane
Added die($newTimestamp); to the code like this:
add_action('cred_save_data', 'calculate_publishing_date',10,2); function calculate_publishing_date($post_id, $form_data) { if ($form_data["id"]==3260) { error_log('Form 3260 has been submitted. The POST superglobal contains: '); error_log(print_r($_POST, true)); if(isset($_POST['wpcf-dodsdato']['timestamp'])){ $timestamp = $_POST['wpcf-dodsdato']['timestamp']; } if(isset($_POST['tidspunkt-for-publisering'])){ $number_of_years = $_POST['tidspunkt-for-publisering']; } $newTimestamp = strtotime('+'.$number_of_years.' year', $timestamp); die($newTimestamp); // $newTimestamp = strtotime('+4 year', $timestamp); (Testing without using $number_of_years works) update_post_meta($post_id, 'wpcf-publiseringsdato', $newTimestamp); } }
I can't see any instances of the $newTimestamp variable in the error log.
I'm attaching the new error log disguised as a jpg. Please rename the field to .txt after download.
Thanks again for looking into this.
Kind regards
Rune
Hi Rune,
Would you mind allowing me to have admin access to the website so that I can have a look at this for you ?
Also could you let me know the page where you are testing this out ?
Thanks,
Shane
Hi Rune,
Could you try testing this again.
The problem was that you didnt add the wpcf- prefix to the "tidspunkt-for-publisering" slug.
This prefix is needed when getting the custom field values from Types.
Please try now and let me know if this helps.
Thanks,
Shane
My issue is resolved now. Thank you!