• Resolved wiverton

    (@wiverton)


    Our organisation has a meeting which is always on the first Friday of the month at 6 pm. I want to include a shortcut to say when the next meeting will be. Using “first Friday of next month” does not work for the following reason:

    For example, let’s say that “today” is Wednesday 2 September. I want the shortcode to return Friday 4 September, but the parameter above would return the first Friday in October.

    So, what I really want is something like “next 6 pm on first Friday of the month” (which I realise is incorrect syntax!), so that if we are already past 6pm on the first Friday of this month, it returns a value for next month, otherwise it returns a value for this month.

    Grateful for any suggestions.

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

    (@denra)

    Hello @wiverton,

    The [wpdts] shortcode cannot be a solution in this special case because it does not support conditions, so you need custom code.

    Please place the following custom code in your child’s theme functions.php file and then use the shortcode [wpdts-monthly-meeting]:

    add_shortcode( 'wpdts-monthly-meeting', 'wpdts_monthly_meeting' );
    function wpdts_monthly_meeting() {
        // Monthly event's day of week and time.
        $meeting_day_of_week = "Friday";
        $meeting_time = "18:00";
        // The current timestamp.
        $ts_now = intval( current_time( 'timestamp', false ) ); 
        // The timestamp of the first day of week of this month.
        $ts_this_month_event = strtotime( "first {$meeting_day_of_week} of this month {$meeting_time}", $ts_now );
        if ( $ts_now <= $ts_this_month_event ) {
            // If the event this month has NOT PASSED use the day of week and time timestamp from this month.
            $ts_event = $ts_this_month_event;
        }
        else {
            // If the event this month has PASSED use the day of week and time timestamp from next month.
            $ts_event = strtotime( "first {$meeting_day_of_week} of next month {$meeting_time}", $ts_now );
        }
        // Display the date and time in a chosen format.
        // More about the date and time formatting symbols on:
        // https://www.php.net/manual/en/datetime.format.php
        return date( "M j, Y G:i", $ts_event );
    }

    Please let us know if that works for you.

    Thread Starter wiverton

    (@wiverton)

    Thank you for the quick response. However, I when I added the new php function in the generally recommended way using the WordPress plugin Code Snippets, it caused my website to crash with the message “There has been a critical error on this website.”, leaving me with no access to the WordPress dashboard. Fortunately, my domain host (GreenGeeks) were able to immediately disable the Code Snippets plugin (along with the supplied function) and the day was saved.

    Plugin Author Denra.com

    (@denra)

    Hello @wiverton,

    The code works correctly when placed in the functions.php file of the child theme. There is no way to test how it will work placed via another plugin. You can see it working on a temporary test page under our domain:
    https://denra.com/tests/test-monthly-meeting/

    Plugin Author Denra.com

    (@denra)

    Here I am pasting absolutely the same working code which is used for the test page on our website, in case you or I have copied the code in the previous post incorrectly:

    add_shortcode( 'wpdts-monthly-meeting', 'wpdts_monthly_meeting' );
    function wpdts_monthly_meeting() {
        // Monthly event's day of week and time.
        $meeting_day_of_week = "Friday";
        $meeting_time = "18:00";
        // The current timestamp.
        $ts_now = intval( current_time( 'timestamp', false ) ); 
        // The timestamp of the first Friday of this month.
        $ts_this_month_event = strtotime( "first {$meeting_day_of_week} of this month {$meeting_time}", $ts_now );
        if ( $ts_now <= $ts_this_month_event ) {
            // If the event this moth has NOT PASSED use the day of week and time timestamp from this month.
            $ts_event = $ts_this_month_event;
        }
        else {
            // If the event this moth has PASSED use the day of week and time timestamp from next month.
            $ts_event = strtotime( "first {$meeting_day_of_week} of next month {$meeting_time}", $ts_now );
        }
        // Display the date and time in a chosen format.
        // More about the date and time formatting symbols on:
        // https://www.php.net/manual/en/datetime.format.php
        return date( "M j, Y G:i", $ts_event );
    }
    Plugin Author Denra.com

    (@denra)

    Dear @wiverton,

    We are closing this support thread.

    Thank you for using WP Date and Time Shortcode.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Next first Friday of the month?’ is closed to new replies.