• Resolved obraun89

    (@obraun89)


    Hello,

    I would like to be able to show a next delivery date on a website, which is every second Saturday. I have looked through your shortcodes but don’t see a way to base the next date based on a starting date or a week of the year.

    The site I’m working on is currently not up, so I can’t show you an example. But for a description, the last delivery date was Dec. 16, 2023. Now when customers come on, I want them to see that the next delivery date is Dec. 30, 2023. I can add next Saturday plus 7 days for this week, but that won’t work next week. From Dec. 17 – 29 I want customers to see that the next delivery date is Dec. 30, 2023. Then from Dec. 30 – Jan. 12 the date shown should be Jan. 13, 2024.

    Is that something that can be done with your plugin? If I could use a week of the year to base the shortcode on, or a set date plus every two weeks, that’s essentially what I’m looking for. Any help would be greatly appreciated.

    Cheers,
    Orlando

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

    (@denra)

    Hello Orlando,

    Here is your custom source code that you need to place in your child theme’s functions.php:

    add_shortcode("wpdts-regular-period", "denra_shortcode_regular_period");
    function denra_shortcode_regular_period($atts, $content, $tag)
    {
    	# Settngs #
    
    	// The current timestamp.
        $ts_now = current_time( 'timestamp', false );
    	// The current date.
    	$date_today = "2023-12-16"; //date( "Y-m-d", $ts_now);
    	// The timestamp for the current date.
    	$ts_today = intval( strtotime($date_today) );
    
    	$atts_default = [
    		'first_day'	=> $date_today, 	// The date you start counting.
    		'period'	=> "14",			// How many days to the next regular day.
            'tag'       => 'span',  		// The default result tag attribute.
    		'class'		=> '',				// CSS class, if needed.
            'format'    => get_option('date_format'),	// You may select a custom one "M j, Y"
    													// More about the date and time formatting symbols on:
    													// https://www.php.net/manual/en/datetime.format.php
        ];
    	// Get the shortcode attributes.
    	$atts = shortcode_atts( 
    		$atts_default,
    		$atts,
    		$tag
    	);
    
    	# Calucations #
    	
    	// The timestamp of the date when you start counting.
    	$ts_first_day = strtotime( $atts['first_day'] );	
    
    	// The magic happens here. :)
    	$ts_days_difference = $ts_today - $ts_first_day;
    	$days_passed = $ts_days_difference / DAY_IN_SECONDS;
    	$days_remnant = $days_passed % $atts['period'];
    	$ts_period_day = $ts_today - ($days_remnant * DAY_IN_SECONDS) + $atts['period'] * DAY_IN_SECONDS;
    	
        // Display the date and time in a chosen format.
        return '<' . $atts['tag'] . ($atts['class'] ? " class=\"{$atts['class']}\"" : "") . '>' . date_i18n( $atts['format'], $ts_period_day ) . '</' . $atts['tag'] . '>';
    }

    Then you may use the shortcode in the following way:

    [wpdts-regular-period first_day="2023-12-16" period="14"]

    You can also change:

    • tag – the HTML tag that is used (span by default)
    • format – the display format (the current WordPress date format for the website by default).
    • class – if you want to add and HTML class to modify via CSS.
    [wpdts-regular-period first_day="2023-12-16" period="14" tag="div" class="my_css_class" format="Y-m-d"]
    Thread Starter obraun89

    (@obraun89)

    Sweet! Just what I need. Thank you so much!

    Orlando

    Plugin Author Denra.com

    (@denra)

    I pasted some test code above by mistake. Please use the full shown below instead.

    add_shortcode("wpdts-regular-period", "denra_shortcode_regular_period");
    function denra_shortcode_regular_period($atts, $content, $tag)
    {
    	# Settngs #
    
    	// The current timestamp.
        $ts_now = current_time( 'timestamp', false );
    	// The current date.
    	$date_today = date( "Y-m-d", $ts_now);
    	// The timestamp for the current date.
    	$ts_today = intval( strtotime($date_today) );
    
    	$atts_default = [
    		'first_day'	=> $date_today, 	// The date you start counting.
    		'period'	=> "14",			// How many days to the next regular day.
            'tag'       => 'span',  		// The default result tag attribute.
    		'class'		=> '',				// CSS class, if needed.
            'format'    => get_option('date_format'),	// You may select a custom one "M j, Y"
    													// More about the date and time formatting symbols on:
    													// https://www.php.net/manual/en/datetime.format.php
        ];
    	// Get the shortcode attributes.
    	$atts = shortcode_atts( 
    		$atts_default,
    		$atts,
    		$tag
    	);
    
    	# Calucations #
    	
    	// The timestamp of the date when you start counting.
    	$ts_first_day = strtotime( $atts['first_day'] );	
    
    	// The magic happens here. :)
    	$ts_days_difference = $ts_today - $ts_first_day;
    	$days_passed = $ts_days_difference / DAY_IN_SECONDS;
    	$days_remnant = $days_passed % $atts['period'];
    	$ts_period_day = $ts_today - ($days_remnant * DAY_IN_SECONDS) + $atts['period'] * DAY_IN_SECONDS;
    	
        // Display the date and time in a chosen format.
        return '<' . $atts['tag'] . ($atts['class'] ? " class=\"{$atts['class']}\"" : "") . '>' . date_i18n( $atts['format'], $ts_period_day ) . '</' . $atts['tag'] . '>';
    }
    Plugin Author Denra.com

    (@denra)

    There was a temporary test code to verify the functionality:

    $date_today = "2023-12-16";

    which now I changed to the current dynamic date:

    $date_today = date( "Y-m-d", $ts_now);

    Unfortunately I can no longer edit the first post.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Show bi-weekly dates based on starting date’ is closed to new replies.