How To Program a Days Until Message in PHP (or WordPress Shortcode)

If you’re in retail or e-commerce, you’re likely always counting down to the upcoming retail holiday to provide a sense of urgency for your audience. Years ago, I implemented a solution like this for a client and wanted to share how easy it is.
PHP: Days Until
Below is a PHP function where you can pass the month, day, and (optionally), three different messages depending on how many days until the day of the year will arrive.
<?php
// Function to calculate the countdown to a specific date
function countdown_to_day($month, $day, $today_message = "Today is the big day!", $tomorrow_message = "It's happening tomorrow!", $countdown_message = "Only X days left!") {
// Get the current year
$current_year = date('Y');
// Create DateTime objects for today and the target date
$today = new DateTime();
$target_date = new DateTime("$current_year-$month-$day");
// If the target date has passed this year, set it for next year
if ($today > $target_date) {
$target_date->modify('+1 year');
}
// Calculate the difference in days
$interval = $today->diff($target_date);
$days_left = $interval->days;
// Return the appropriate response
if ($days_left === 0) {
return $today_message;
} elseif ($days_left === 1) {
return $tomorrow_message;
} else {
return str_replace("X", $days_left, $countdown_message);
}
}
Explanation for the Function
The countdown_to_day
function is designed to calculate the number of days until a specified date and return a custom message based on the proximity to that date. It accepts the following parameters:
$month
and$day
: Specify the target date (month and day).$today_message
: The message displayed when the target date is today. Defaults to “Today is the big day!”.$tomorrow_message
: The message displayed when the target date is tomorrow. Defaults to “It’s happening tomorrow!”.$countdown_message
: The message displayed for all other dates. The placeholderX
is dynamically replaced with the number of days remaining. Defaults to “Only X days left!”.
The function calculates the target date relative to the current year. If the specified date has already passed this year, it adjusts the target to the following year. Based on the days remaining:
- If it’s the target date, the
$today_message
is returned. - If it’s the day before, the
$tomorrow_message
is returned. - For other cases, the
$countdown_message
is returned withX
replaced by the actual number of days left.
This function is ideal for dynamically creating event reminders or countdowns with personalized messages.
WordPress Shortcode
If you want to utilize this in WordPress, add the above code and the code below into a custom plugin. Plugins are advantageous over using your theme or child theme functions because you can still update your theme, and the content will continue to function correctly.
// WordPress shortcode to use the countdown function
function countdown_shortcode($atts) {
// Extract attributes with default values for Christmas (December 25)
$atts = shortcode_atts(
[
'month' => 12,
'day' => 25,
'today_message' => 'Today is the big day!',
'tomorrow_message' => "It's happening tomorrow!",
'countdown_message' => "Only X days left!",
],
$atts
);
// Ensure month and day are integers
$month = intval($atts['month']);
$day = intval($atts['day']);
$today_message = sanitize_text_field($atts['today_message']);
$tomorrow_message = sanitize_text_field($atts['tomorrow_message']);
$countdown_message = sanitize_text_field($atts['countdown_message']);
// Call the countdown function and return the result
return countdown_to_day($month, $day, $today_message, $tomorrow_message, $countdown_message);
}
// Register the shortcode with WordPress
add_shortcode('countdown', 'countdown_shortcode');
Explanation for the Shortcode
The countdown_shortcode
function integrates the countdown_to_day
functionality into WordPress using a shortcode. This allows content creators to easily include countdowns in posts or pages.
Attributes:
month
: Specifies the target month (default is 12 for December).day
: Specifies the target day (default is 25 for Christmas).today_message
: Custom message for when the target date is today (e.g., “Happy Birthday!”).tomorrow_message
: Custom message for when the target date is tomorrow (e.g., “It’s Christmas Eve!”).countdown_message
: Customizable countdown message withX
replaced by the number of days left (e.g., “Only X days until the event!”).
How to Use: In a WordPress post or page, use the shortcode like this:
[countdown month="12" day="25" today_message="Merry Christmas!" tomorrow_message="It's Christmas Eve!" countdown_message="Only X days until Christmas!"]
Output Examples:
- On December 25: “Merry Christmas!”
- On December 24: “It’s Christmas Eve!”
- On December 20: “Only 5 days until Christmas!”
Here’s the output:
Only 244 more days until Christmas!
This shortcode makes providing dynamic, personalized event reminders in WordPress content easy. It supports full customization for various occasions and is a valuable tool for engaging users.