How to send Notification conditionally based on day of week and time the form was completed?

How can I configure my GF form notifications to be sent to an admin (emailed) on Monday-Friday, from 5-7pm and on Saturdays and Sundays from 9am-3pm.

To be clear, the notifications are already being sent to an admin upon form completion, but I need to conditionally have completed forms sent to an admin (emailed) on Monday-Friday, from 5-7pm and on Saturdays and Sundays from 9am-3pm.

Is there an existing solution for this out there??

Thanks!!

Hi Donald, I think you previously submitted a support ticket about the same issue? I recommended using the gform_notification filter.

Can you clarify WHEN the notification needs to be overridden with a new email address? From 5-7p Mon - Fri and 9a-3p Sunday, they are sent to some address. Where are they sent at the other times, and is it always the same notification, just being one place or another based on the date and time?

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

I think I have some code figured out for you. Your original support request mentioned Saturday and Sunday for the hours of 9am-3pm, so that’s how I wrote this.

// target form id 116; change to your form id
add_filter( 'gform_notification_116', 'set_email_to_address', 10, 3 );
function set_email_to_address( $notification, $form, $entry ) {
        GFCommon::log_debug( __METHOD__ . '(): The Notification => ' . print_r( $notification, true ) );
        // change 'Admin Notification' here to the actual name of your notification
        // only consider changing the TO address for the Admin Notification
        if ( $notification['name'] == 'Admin Notification' ) {
                // set email to address based on current date and time
                // when 5-7pm Mon - Fri and 9a-3pm Saturday and Sunday
                // https://codex.wordpress.org/Function_Reference/current_time
                $timestamp = current_time( 'timestamp' );
                // http://docs.php.net/getdate
                $d = getdate( $timestamp );
                GFCommon::log_debug( __METHOD__ . '(): The getdate => ' . print_r( $d, true ) );
                $hour = $d['hours'];
                $day = $d['wday'];
                // if day is Saturday(6) or Sunday(0)
                if ( $day == 0 || $day == 6 ) {
                        GFCommon::log_debug( __METHOD__ . '(): It\'s Saturday or Sunday.' );
                        // if hour is between 9 and 15
                        // 14 ends at 2:59
                        if ( $hour >= 9 && $hour < 15 ) {
                                GFCommon::log_debug( __METHOD__ . '(): It\'s between 9AM and 3PM.' );
                                $notification['to'] = 'specialaddress@example.com';
                        }
                }
                // if day is between 1 and 5
                if ( $day >=1 && $day <= 5 ) {
                        GFCommon::log_debug( __METHOD__ . '(): It\'s between Monday and Friday.' );
                        // if hour is between 17 and 18
                        // 18 ends at 18:59
                        if ( $hour >= 17 && $hour < 18 ) {
                                GFCommon::log_debug( __METHOD__ . '(): It\'s between 5PM and 7PM.' );
                                $notification['to'] = 'specialaddress@example.com';
                        }
                }
        }
        // return altered notification object
        GFCommon::log_debug( __METHOD__ . '(): The Modified Notification => ' . print_r( $notification, true ) );
        return $notification;
}

You will need to change the form ID from 116 to your own form ID. I have a lot of logging statements in there which will only be useful if you have logging enabled (Forms > Settings > Logging > Enable logging for all messages for Gravity Forms Core)

Let me know if you have any questions. I could not test for all possible days and times but I believe the logic is correct.

[SITE REMOVED]

1 Like

Hi Chris and THANKS for your due diligence and code!!! I suppose this goes in my functions.php file, correct?

And is this your final crack it this? I wasn’t sure whether you were finished with it or not.

Thank you!!!

Yes, the code can go into the theme functions.php file. The code will work and is complete. You can comment out the logging statements after you’re satisfied this is working as expected. Let me know how it turns out.

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

Can you please email me the complete functions.php file as an email attachment to chris@rocketgenius.com thank you

I received the file. I added the code and the syntax is fine. What is the version of PHP in use on your server and what is your Gravity Forms version? That error can come from older versions of PHP.

Thanks for your reply.
My server runs PHP version 7.0
Gravity Forms is ver. 2.3.6

Thanks for all your great work, Chris!! Looks like it’s working! You’re the best.

1 Like

resolved via email

Thank you Donald. I’ll leave this open in case an issue arises or someone wants to piggyback onto the conversation.