I am struggeling with a live countdown on my auction site. (Live countdowns are not really possible with PHP so I need to use javascript. but I dont really know where to start.)
I currently have this code that displays how much time before the auction ends but this is not really accurate and I don't really know how to fix this.
I saw someone else with a live countdown (Countdown For WooCommerce Products)
But I don't really know how I can implement this code with my date. I tried to use just static dates for testing but this also did not work for me.
What is the best option for me? and what should be the best en most relaible fix?
function auction_end_date_countdown() {
global $product;
$product = apply_filters( 'yith_wcact_get_auction_product', $product );
ob_start();
if ( 'auction' === $product->get_type() ) {
$dateclose = $product->get_end_date();
if ( $dateclose ) {
$today = date("Y-m-d H:i:s");
$format_date = get_option( 'yith_wcact_general_date_format', 'Y-m-d H:i:s' );
$format = $format_date . ' ' . $format_time;
$date = ( date( 'Y-m-d H:i:s', $dateclose ) );
$diff = abs(strtotime($today) - strtotime($date));
$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));
$hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
$minuts = 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 - $minuts*60));
printf("%d months, %d days, %d hours", $months, $days, $hours);
}
}
$html = ob_get_clean();
return $html;
}
add_shortcode( 'veiling_eind_datum_countdown', 'auction_end_date_countdown' );