Year change only after annual event held

[ad_1]

This is a very specific use case @paulryken .

You can use this into your child theme’s functions.php or Code Snippets plugin.


add_shortcode('specialdate', 'dmyip_rmd_specialdate');
function dmyip_rmd_specialdate($atts) {
// Set default attributes and merge with user input
$attributes = shortcode_atts(array(
'floor_date' => 'today',
'ceiling_date' => 'last sunday of january next year'
), $atts);

// Parse the floor date using strtotime
$floor_date = strtotime($attributes['floor_date']);
if (!$floor_date) {
return ''; // Clear any error messages
}

// Parse the ceiling date using strtotime
$ceiling_date = strtotime($attributes['ceiling_date']);
if (!$ceiling_date) {
return ''; // Clear any error messages
}

// Calculate the ceiling date next year
$ceiling_date_next_year = strtotime('+1 year', $floor_date);

// Get today's date
$today = time();

// Determine if today's date is between the floor date and the ceiling date next year
if ($today >= $floor_date && $today <= $ceiling_date_next_year) {
// Format the ceiling date with day, month, and year
$display_date = date_i18n('j F Y', $ceiling_date);
return esc_html($display_date);
}

return '';
}

add_shortcode('specialyear', 'dmyip_rmd_specialyear');
function dmyip_rmd_specialyear($atts) {
// Set default attributes and merge with user input
$attributes = shortcode_atts(array(
'floor_date' => 'today',
'ceiling_date' => 'last sunday of january next year'
), $atts);

// Parse the floor date using strtotime
$floor_date = strtotime($attributes['floor_date']);
if (!$floor_date) {
return ''; // Clear any error messages
}

// Parse the ceiling date using strtotime
$ceiling_date = strtotime($attributes['ceiling_date']);
if (!$ceiling_date) {
return ''; // Clear any error messages
}

// Calculate the ceiling date next year
$ceiling_date_next_year = strtotime('+1 year', $floor_date);

// Get today's date
$today = time();

// Determine if today's date is between the floor date and the ceiling date next year
if ($today >= $floor_date && $today <= $ceiling_date_next_year) {
// Format the ceiling date to show just the year
$display_year = date_i18n('Y', $ceiling_date);
return esc_html($display_year);
}

return '';
}

It has two components.

[specialdate ceiling_date="last sunday of january next year"] will show the full date like 28 January 2025.

[specialyear ceiling_date="last sunday of january next year"] will just show 2025.

In the ceiling_date argument you can ask for dates in natural language.

Let me know if this works.

I will be adding this to the plugin as well but only after someone else asks for it.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer