How can I get the time remaining for an event to appear in few months time

Question

I am looking forward to embedding countdown dates in my wordpress post editor via wp shortcodes.

I am not so good in coding, so i have come across already written codes but they require me to input the year (which i think is bot necessary).

The date for each event’s occurrence will differ from one post to another. For example: if my target is 31st December, it will get me the weeks, days and hours remaining for such event to occur.

On January 1st, it is expected to start afresh the count. That is just what i want.

The shortcode i have tried so far in my functions.php is

function content_countdown($atts, $content = null){
extract(shortcode_atts(array(
‘month’ => ”,
‘day’ => ”,
‘year’ => ”
), $atts));
$remain = ceil((mktime( 0,0,0,(int)$month,(int)$day,(int)$year) – time())/86400);
if( $remain > 1 ){
return $daysremain = "<div class="event">Just ($remain) days until content is available";
}else if($remain == 1 ){
return $daysremain = "<div class="event">Just ($remain) day until content is available";
}else{
return $content;
}
}
add_shortcode(‘cdt’, ‘content_countdown’);

So that when i embed using, [cdt month="8" day="12" year="2021"] it shows only number of days remaining but
The problem with the code is that

  • It must be a future year date
  • It shows only the days left. Weeks and hours not included.
0
Jaybliss 2 months 0 Answers 8 views 0

Leave an answer