• Resolved chris15326

    (@chris15326)


    Hey! I just wonder if there is a way to change / edit the plugins code that it adds (to the function.php?) in order to grab the current time + add something like 30 minutes in 15 minute intervals.

    So that the result will always be XX:00 XX:15 XX:30 XX:45 while using 24h format.

    For example: if it’s 09:10 it would show 09:30 or 09:45.

    With kind regards
    Chris

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

    (@denra)

    Hi @chris15326,

    Let me think about a custom solution. I will write back soon (in a couple of days).

    Regards,
    Ivaylo Tinchev
    Denra.com

    Plugin Author Denra.com

    (@denra)

    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.

    Plugin Author Denra.com

    (@denra)

    Hello @chris15326,

    I am closing the support thread. Please reopen it again if needed.

    Plugin Author Denra.com

    (@denra)

    Closed.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Edit the plugin code snippet’ is closed to new replies.