• Resolved AlexPR

    (@alexpr)


    I would like to subtract a specific number of days from today’s date. The number of days to subtract is stored in an advanced custom field which I can retrieve by its shortcode, e.g. [acf field="delta"]. In this example, this field contains -30 as a negative number.
    How can I use this custom field as a variable in the [wpdts] shortcode?

    This is what I tried without success:

    [wpdts days=[acf field="delta"]]

    and as nested shortcodes:

    [wpdts days=]
    [acf field="delta"]
    [/wpdts]

    How can I use a variable or shortcode to calculate a relative date?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Denra.com

    (@denra)

    Hi @alexpr,

    WordPress does not support “shortcode in shortcode” method of display, so you need a custom code for the purpose. You may place such in the functions.php file of your child theme to create your own shortcode.

    1. Without using WP Date and Time Shortcode (you don’t need to have it installed):

    // Add the shortcode with custom function without using [wpdts]
    add_shortcode( 'denra-delta', 'denra_delta_no_wpdts' ); 
    function denra_delta_no_wpdts ( $atts, $content='', $tag ) {
       // Get the current timestamp with offset according to the WP timezone.
       $ts_current = current_time( 'timestamp' );
       // The number of days difference in seconds. 86400 sec = 1 day.
       $ts_diff = intval( do_shortcode( '[acf field="delta"]' ) ) * 86400;
       // The result timestamp.
       $ts_res = $ts_current + $ts_diff;
       // Return the date in the wanted format without [wpdts]
       return date( 'Y-m-d H:i:s', $ts_res );
    }

    2. With using WP Date and Time Shortcode (you need to have it installed and active):

    // Add the shortcode with custom function using [wpdts]
    add_shortcode( 'denra-delta', 'denra_delta_wpdts' );
    function denra_delta_wpdts ( $atts, $content='', $tag ) {
       // The number of days difference from the ACF shortcode.
       $days = intval( do_shortcode( '[acf field="delta"]' ) );
       $days_attr = ( $days < 0 ) ? $days : '+' . $days;
       // Return the date in the wanted format with [wpdts-custom]
       return do_shortcode( '[wpdts-custom format="Y-m-d H:i:s" days="' . $days_attr . ' days"]' );
    }

    Then use [denra-delta] in your website content.

    You may change the date format in the quotes (‘Y-m-d H:i:s’) as you wish using the symbols listed here:
    https://www.php.net/manual/en/datetime.format.php

    Please let me me know if that works for you.

    Plugin Author Denra.com

    (@denra)

    PS: Please note that the latest code was slightly edited. Please use it as it is in the post now.

    Thread Starter AlexPR

    (@alexpr)

    Your code works great. Thank You!

    Would it be possible to put the name of the custom field (delta) which contains the days as a parameter when calling the shortcode and to specify if the number of days should be added (+) or subtracted (-) from today’s date?
    As a super convenient extra bonus, it would be great if a constant number could be added or subtracted in addition to the delta range, e.g.

    [denra-delta +1 "delta"] which would add 1 day plus delta days to todays date
    and
    [denra-delta -2 "delta"] which would subtract 2 days plus delta days from todays date

    • This reply was modified 3 years, 7 months ago by AlexPR.
    Plugin Author Denra.com

    (@denra)

    Hello,

    The code above can be modified in many ways but you need to ask another PHP programmer to do it for you. We do not offer such services. At least you have the solution you asked for in the first post.

    You can additionally use the start attribute to modify the calculation start time for the case with the [wpdts] shortcode or change the calculations of the timestamps in the functions for the other case.

    I am closing the thread as resolved.

    Good luck!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Subtract number of days (stored in custom field) from current date’ is closed to new replies.