Content MarketingE-commerce and Retail

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:

  1. $month and $day: Specify the target date (month and day).
  2. $today_message: The message displayed when the target date is today. Defaults to “Today is the big day!”.
  3. $tomorrow_message: The message displayed when the target date is tomorrow. Defaults to “It’s happening tomorrow!”.
  4. $countdown_message: The message displayed for all other dates. The placeholder X 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 with X 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:

  1. month: Specifies the target month (default is 12 for December).
  2. day: Specifies the target day (default is 25 for Christmas).
  3. today_message: Custom message for when the target date is today (e.g., “Happy Birthday!”).
  4. tomorrow_message: Custom message for when the target date is tomorrow (e.g., “It’s Christmas Eve!”).
  5. countdown_message: Customizable countdown message with X 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.

Appreciate this content?

Sign up for our weekly newsletter, which delivers our latest posts every Monday morning.

We don’t spam! Read our privacy policy for more info.

Douglas Karr

Douglas Karr is CMO of OpenINSIGHTS and the founder of the Martech Zone. Douglas has helped dozens of successful MarTech startups, has assisted in the due diligence of over $5 bil in Martech acquisitions and investments, and continues to assist companies in implementing and automating their sales and marketing strategies. Douglas is an internationally recognized digital transformation and MarTech expert and speaker. Douglas is also a published author of a Dummie's guide and a business leadership book.

Related Articles

Back to top button
Close

Adblock Detected

Martech Zone is able to provide you this content at no cost because we monetize our site through ad revenue, affiliate links, and sponsorships. We would appreciate if you would remove your ad blocker as you view our site.