Hello again @chris15326 ,
Please place the code below in the functions.php of your child theme:
add_shortcode( 'wpdts-custom-intervals', 'wpdts_custom_intervals' );
function wpdts_custom_intervals( $atts_set, $content, $tag ) {
// Default attributes for the shortcode.
$atts_default = array(
'start' => current_time( 'mysql', false ), // The time to show.
'format' => 'M j, Y G:i', // The final format with PHP date() symbols.
'intervals' => '00,15,30,45', // In minutes of the hour.
);
// All final attributes.
$atts = shortcode_atts(
$atts_default,
$atts_set,
$tag
);
// The current timestamp according to the 'start' attribute.
$ts_start = intval( strtotime( $atts['start'] ) );
// Timestamp still not rounded.
$ts_final = $ts_start;
// Convert the interval attribute values to an array of integers.
$intervals_arr = explode( ',', $atts['intervals'] );
if ( count( $intervals_arr ) ) { // In case we have intervals set we search.
// Convert the intervals minitues strings to timestamps.
$ts_intervals_arr = array();
foreach( $intervals_arr as $value ) {
// In case we have 0 or passed minutes then switch to next hour.
$ts_modified = ( 0 == $value || date( "i", $ts_start ) >= $value ) ? $ts_start + HOUR_IN_SECONDS : $ts_start;
// Calculate the intervals timestamps.
$ts_intervals_arr[] = strtotime( date( "Y-m-d H:" . sprintf( '%02d', $value ) . ":00", $ts_modified ) );
}
// Sort the intervals array in reverse order.
rsort( $ts_intervals_arr );
// Let us keep the previous interval timestamp value.
$ts_interval_prev = array_shift( $ts_intervals_arr );
foreach( $ts_intervals_arr as $ts_interval ) {
if ( $ts_start >= $ts_interval ) {
//The closest interval timestamp is found.
break;
}
else {
// Keep the closer interval timestamp.
$ts_interval_prev = $ts_interval;
}
}
$ts_final = $ts_interval_prev;
}
return date( $atts['format'], $ts_final );
}
Then try it with the following shortcode:
[wpdts-custom-intervals]
.
You can also use custom format
and intervals
attributes instead of the default ones like this:
[wpdts-custom-intervals format="Y-m-d H:i:s" intervals="00,30"]
.
Please let me know if it works for you.