0

I am trying to create a count down timer to an event (linked to a user) in Wordpress.

So far I have managed to create the count down using jquery by manually entering the date of the event. Fiddle

However I want this date to be dynamically used based on when the users event is i.e. using the custom meta field for date for the user. Please help in terms on how I can achieve this by calling the custom meta-field.

Jquery:

// set the date we're counting down to
var target_date = new Date("Aug 15, 2019").getTime();

// variables for time units
var days, hours, minutes, seconds;

// get tag element
var countdown = document.getElementById("countdown");

// update the tag with id "countdown" every 1 second
setInterval(function () {

// find the amount of "seconds" between now and target
var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 1000;

// do some time calculations
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;

hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;

minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);

// format countdown string + set tag value
countdown.innerHTML = days + "d," + hours + "h," + minutes + "m," + seconds + "s";   

}, 1000);

HTML:

<span id="countdown"></span>
4

2 回答 2

0

好的,我设法弄清楚了。因此,根据 Javascript,如果我手动将日期添加到var target_date = new Date("Aug 15, 2019").getTime();.

然后我需要收集每个用户的事件日期并在这个函数中替换它。为了获取存储在用户元数据中的事件日期,我使用了以下代码:

<?php $user_id = get_current_user_id(); $key = 'Your Event Date'; $single = true; $user_last = get_user_meta( $user_id, $key, $single ); echo $user_last; ?>

然后我在 Javascript 中添加了这个函数来让倒计时工作:

var target_date = new Date( '<?php $user_id = get_current_user_id(); $key = 'Event Date';    $single = true; $user_last = get_user_meta( $user_id, $key, $single ); echo $user_last; ?>' ).getTime();
于 2013-11-01T07:22:28.333 回答
0

目前尚不完全清楚您要做什么,但我认为是这样的。

如果您可以在脚本标签之间输出如下内容。

Javascript:

countdownDate = 'Aug 15, 2019';

并且像您已经在其他地方使用它。

Javascript:

// set the date we're counting down to
var target_date = new Date(countdownDate).getTime();
于 2013-10-31T11:52:44.293 回答