How to display time remaining until the WooCommerce product expiration date in WordPress

The post titled "How to set and display the WooCommerce product expiration date and time in WordPress" explains how to set and display the product expiration date in WooCommerce. In this post, I will discuss how to display the time left until the product end date.

It's possible to display time remaining by comparing the post expiration date (set by Post Expirator) and the current date. We need to modify a file of the Post Expirator plugin: post-expirator.php (located under the Post Expirator plugin folder.)

  1. Open the "post-expirator.php " file within the Post Expirator plugin folder
  2. Go to the code "function postexpirator_shortcode($atts)"
  3. Copy the "function postexpirator_shortcode($atts)" code snippet and paste it immediately under this function
  4. Change the function name and shortcode (For example, I changed "postexpirator" to "postexpiratordiffer". You can change it as you wish.)
  5. Change "return get_date_from_gmt(gmdate('Y-m-d H:i:s',$expirationdatets),$format);" to "return gmdate('Y-m-d H:i:s',$expirationdatets);" Or you can change it as shown:
    postexpirator revised

Now, we need to add a code to compare the expiration date and the current dat and then display the time left to the WooCommerce template file: short-description.php (located under "/woocommerce/wp-content/plugins/woocommerce/templates/single-product/" folder.)

You can use the following code for the WooCommerce product expiration date and the current date:

$expriationdate = do_shortcode( '[postexpiratordiffer]' );
$now = date('Y-m-d H:i:s');

You can use the code found on this page for date difference.

$expriationdate = do_shortcode( '[postexpiratordiffer]' );
$now = date('Y-m-d H:i:s');
$diff = abs(strtotime($now) - strtotime($expriationdate));
$years = floor($diff / (365*60*60*24));
...

Please replace "..." with the code snippet of the function "diff()" found on the page "Date difference between two timezones in PHP."

An exmaple showing time remaining until the product end date:

How to display time remaining until the WooCommerce product expiration date

Reference

The final code will be similar to:

<?php
$tempCheck = do_shortcode( '[postexpirator]' );
if(empty($tempCheck)) {
echo '<div class="expriacy-date">Expired on: None</div>';
} else {
echo '<div class="expiracy-date">Expired on ' . do_shortcode( '[postexpirator]' ) . '</div>';


$expriationdate = do_shortcode( '[postexpiratordiffer]' );
$now = date('Y-m-d H:i:s');

echo '<div id="autoexpirationdate" style="font-size: 12px; color: red">(';

$diff = abs(strtotime($now) - strtotime($expriationdate));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$days2 = floor(($diff - $years * 365*60*60*24)/ (60*60*24));
$hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
$minutes = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ (60));
$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minutes*60));

printf("%d Dates %d Hours %d Minutes %d Seconds\n", $days2, $hours, $minutes, $seconds);

echo ' left)</div>';

}
?>

Update: Instead of editing the WooCommerce files, you can use a filter:

// Displaying the expiration date for WooCommerce Products

function woocommerce_expiration_date ( $desc ){
global $product;

if ( is_single( $product->id ) ){

$tempCheck = do_shortcode( '[postexpirator]' );
if(empty($tempCheck)) {
$desc .= '<div class="expriacy-date">Expired: None</div>';
} else {
$desc .= '<div class="expiracy-date">Expired: ' . do_shortcode( '[postexpirator]' ) . '</div>';

$expriationdate = do_shortcode( '[postexpiratordiffer]' );
$now = date('Y-m-d H:i:s');

$desc .= '<div id="autoexpirationdate" style="font-size: 12px; color: red">(';

$diff = abs(strtotime($now) - strtotime($expriationdate));

// same with the above code (from $years through $minutes lines)
$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minutes*60));

$desc .= $days2 . 'days ' . $hours . 'hours  ' . $minutes . 'minutes  ' . $seconds . 'seconds';

$desc .= ' left)</div><br>';
}
return $desc;
}
}
add_filter( 'woocommerce_short_description', 'woocommerce_expiration_date' );

3 개 댓글

  1. Hello there,

    I have done everything you write but i got wrong countdown. E.g the product expires in 9 hours

    but instead of :  a "0 days 9 hours 0 minutes 0 seconds"

    i see ": 0 days 0 hours 0 minutes 3 seconds
    "

    What i have to check?

    Thank you

Leave a Comment

3s