Skip to content

Instantly share code, notes, and snippets.

@Idealien
Last active March 28, 2020 04:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Idealien/55cd37f96936637f5838f7267d7bde4a to your computer and use it in GitHub Desktop.
Save Idealien/55cd37f96936637f5838f7267d7bde4a to your computer and use it in GitHub Desktop.
Preliminary Business Hour Notification Example
<?php
add_filter( 'gravityflow_step_schedule_timestamp', 'schedule_business_hours', 10, 3 );
function schedule_business_hours( $schedule_timestamp, $schedule_type, $step ) {
//Ensure you are only adjusting the desired form/step
if ( $step->get_id() !== 74 ) {
return $schedule_timestamp;
}
gravity_flow()->log_debug( __METHOD__ . '(): Original Scheduled: ' . date( 'Y-m-d H:i:s', $schedule_timestamp ) );
//Define for your business irrespective of time-zone
$start_hour = 9;
$end_hour = 17;
//Wed Mar 21 @ 10:30 - 1521642600
//$current_timestamp = 1521642600;
//Wed Mar 21 @ 21:00 - 1521680400
//$current_timestamp = 1521680400;
//Fri Mar 23 @ 22:15 - 1521857700
//$current_timestamp = 1521857700;
//Sat Mar 24 @ 12:00 - 1521907200
//$current_timestamp = 1521907200;
//Sun Mar 25 @ 20:00 - 1522022400
//$current_timestamp = 1522022400;
//Mon Mar 26 @ 05:15 - 1522055700
//$current_timestamp = 1522055700;
//Mon Mar 26 @ 09:30 - 1522071000
//$current_timestamp = 1522071000;
$current_timestamp = time();
$tz = get_option('timezone_string');
$dt = new DateTime("now", new DateTimeZone($tz));
$dt->setTimestamp($current_timestamp);
$current_hour = $dt->format('H');
$current_day_of_week = $dt->format('N');
gravity_flow()->log_debug( __METHOD__ . '(): Current Timestamp: ' . print_r( $current_timestamp, true ) );
gravity_flow()->log_debug( __METHOD__ . '(): Current Time: ' . $dt->format('d.m.Y, H:i') );
gravity_flow()->log_debug( __METHOD__ . '(): Current Hour: ' . $current_hour );
gravity_flow()->log_debug( __METHOD__ . '(): Current Day Of Week: ' . $current_day_of_week );
gravity_flow()->log_debug( __METHOD__ . '(): Start / End: ' . $start_hour . ' / ' . $end_hour );
//Weekday + Business Hours
if ( in_array( $current_day_of_week, array( 1, 2, 3, 4, 5 ) ) && $current_hour >= $start_hour && $current_hour <= $end_hour ) {
gravity_flow()->log_debug( __METHOD__ . '(): Business Hour Request');
return $schedule_timestamp;
}
gravity_flow()->log_debug( __METHOD__ . '(): Outside Business Hour Request' );
//Weekday + Before Business Hours
if( in_array( $current_day_of_week, array( 1, 2, 3, 4, 5 ) ) && $current_hour <= $start_hour ) {
$delay_hours = $start_hour - $current_hour;
//Mon to Thurs + After Business Hours
} else if( in_array( $current_day_of_week, array( 1, 2, 3, 4 ) ) && $current_hour >= $end_hour ) {
$delay_hours = ( 23 - $current_hour ) + 1 + $start_hour;
//Friday + After Business Hours
} else if( in_array( $current_day_of_week, array( 5 ) ) && $current_hour >= $end_hour ) {
$delay_hours = ( 23 - $current_hour ) + 1 + $start_hour + 48;
//Saturday
} else if( in_array( $current_day_of_week, array( 6 ) ) ) {
$delay_hours = ( 23 - $current_hour ) + 1 + $start_hour + 24;
//Sunday
} else if( in_array( $current_day_of_week, array( 7 ) ) ) {
$delay_hours = ( 23 - $current_hour ) + 1 + $start_hour;
//Edge case handling
} else {
$delay_hours = 0;
}
gravity_flow()->log_debug( __METHOD__ . '(): Delay Hours ' . $delay_hours );
$schedule_timestamp = $current_timestamp + ( $delay_hours * 60 * 60 );
gravity_flow()->log_debug( __METHOD__ . '(): Adjusted Scheduled: ' . date( 'Y-m-d H:i:s', $schedule_timestamp ) );
return $schedule_timestamp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment