0

我想不通,希望有一个简单的解决方案,但我看不出这个脚本在哪里失败。该脚本是 wp 插件的一部分,但它失败的地方没有 wp 代码。

它的作用是计算到结束日期并将其打印在屏幕上。该行:

金额 = (dateFuture.getTime() - dateNow.getTime()+5);

显示为负数,所以不可能,未来日期是“2014-10-23 23:00:00”,而我国当前日期和时间是“2014-10-23 20:00:00”不能< 0 如果 dateFuture - dateNow。代码在实际日期(2014-10-23)但不是小时时失败。即实际日期总是显示 Day: 0, Hour: 0, Mins: 0, Secs: 0。如果这一天是未来的一天,它会正常工作。

非常感谢您提前提供的帮助和时间。

function countdown_shortcode_handler( $args, $content = null ){
$dateEnd = date("Y-m-d H:i:s", strtotime("now +3 hours"));
$theme = "default";
$tz = false; //"America/Los_Angeles";

if(isset($args["date"])){ $dateEnd = $args["date"]; }
if(isset($args["theme"])){ $theme = $args["theme"]; }
if(isset($args["timezone"])){ $tz = $args["timezone"]; }

ob_start();
if($tz){
    $oldtz = date_default_timezone_get();
    date_default_timezone_set($tz); // Change locale
}

$strTime = strtotime($dateEnd);
$randID = md5(rand(9000, 10000));
?>
<link href="<?php print plugins_url('countdown/theme/' . $theme . '/style.css') ?>" type="text/css" rel="stylesheet" />
<div id="<?php print $randID ?>" class="countdown"><?php print date("l jS \of F Y h:i:s A", $strTime) ?></div>

<script>
var dateFuture = new Date(<?php print date("Y", $strTime) ?>,<?php print (date("m", $strTime)-1) ?>,<?php print date("d", $strTime) ?>,<?php print date("h", $strTime) ?>,<?php print date("i", $strTime) ?>,<?php print date("s", $strTime) ?>);

function count<?php print $randID ?>timer(){
dateNow = new Date();
amount = (dateFuture.getTime() - dateNow.getTime()+5);
document.write(amount);
delete dateNow;
if(amount < 0){
    jQuery("#<?php print $randID ?> .days .number").html("0");
    jQuery("#<?php print $randID ?> .hour .number").html("0");
    jQuery("#<?php print $randID ?> .mins .number").html("0");
    jQuery("#<?php print $randID ?> .secs .number").html("0");
}else{
    days = 0;
    hours = 0;
    mins = 0;
    secs = 0;
    amount = Math.floor(amount / 1000);
    days = Math.floor(amount / 86400);
    amount = amount % 86400;
    hours = Math.floor(amount / 3600);
    amount = amount % 3600;
    mins = Math.floor(amount / 60);
    amount = amount % 60;
    secs = Math.floor(amount);
    if(jQuery("#<?php print $randID ?> .days .number").html()!=days){
        jQuery("#<?php print $randID ?> .days .number").html(days);
    }
    if(jQuery("#<?php print $randID ?> .hour .number").html()!=hours){
        jQuery("#<?php print $randID ?> .hour .number").html(hours);
    }
    if(jQuery("#<?php print $randID ?> .mins .number").html()!=mins){
        jQuery("#<?php print $randID ?> .mins .number").html(mins);
    }
    if(jQuery("#<?php print $randID ?> .secs .number").html()!=secs){
        jQuery("#<?php print $randID ?> .secs .number").html(secs);
    }

    if(days==0){ jQuery("#<?php print $randID ?> .days").hide(); }

    setTimeout("count<?php print $randID ?>timer()", 1000);
}
}
jQuery(document).ready(function(){
var holder_stuff = '<span class="number"></span><span class="over"></span><span class="title"></span>';
var holder_days = jQuery("<span>").addClass("days").addClass("item").html(holder_stuff);
var holder_hour = jQuery("<span>").addClass("hour").addClass("item").html(holder_stuff);
var holder_mins = jQuery("<span>").addClass("mins").addClass("item").html(holder_stuff);
var holder_secs = jQuery("<span>").addClass("secs").addClass("item").html(holder_stuff);
jQuery("#<?php print $randID ?>").html("").append(holder_days).append(holder_hour).append(holder_mins).append(holder_secs);
count<?php print $randID ?>timer();
});
</script>
<?php
if($tz){
    date_default_timezone_set($oldtz); // Restore current time
}
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode('countdown', 'countdown_shortcode_handler');

function countdown_wp_enqueue_scripts(){
wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'countdown_wp_enqueue_scripts');

?>

生成的Javascript:

<div id="951124d4a093eeae83d9726a20295498" class="countdown">Thursday 23rd of October 2014 11:00:00 PM</div>

<script>
var dateFuture = new Date(2014,9,23,11,00,00);

function count951124d4a093eeae83d9726a20295498timer(){
dateNow = new Date();
amount = (dateFuture.getTime() - dateNow.getTime()+5);

delete dateNow;
if(amount < 0){
    jQuery("#951124d4a093eeae83d9726a20295498 .days .number").html("0");
    jQuery("#951124d4a093eeae83d9726a20295498 .hour .number").html("0");
    jQuery("#951124d4a093eeae83d9726a20295498 .mins .number").html("0");
    jQuery("#951124d4a093eeae83d9726a20295498 .secs .number").html("0");
}else{
    days = 0;
    hours = 0;
    mins = 0;
    secs = 0;
    amount = Math.floor(amount / 1000);
    days = Math.floor(amount / 86400);
    amount = amount % 86400;
    hours = Math.floor(amount / 3600);
    amount = amount % 3600;
    mins = Math.floor(amount / 60);
    amount = amount % 60;
    secs = Math.floor(amount);
    if(jQuery("#951124d4a093eeae83d9726a20295498 .days .number").html()!=days){
        jQuery("#951124d4a093eeae83d9726a20295498 .days .number").html(days);
    }
    if(jQuery("#951124d4a093eeae83d9726a20295498 .hour .number").html()!=hours){
        jQuery("#951124d4a093eeae83d9726a20295498 .hour .number").html(hours);
    }
    if(jQuery("#951124d4a093eeae83d9726a20295498 .mins .number").html()!=mins){
        jQuery("#951124d4a093eeae83d9726a20295498 .mins .number").html(mins);
    }
    if(jQuery("#951124d4a093eeae83d9726a20295498 .secs .number").html()!=secs){
        jQuery("#951124d4a093eeae83d9726a20295498 .secs .number").html(secs);
    }

    if(days==0){ jQuery("#951124d4a093eeae83d9726a20295498 .days").hide(); }

    setTimeout("count951124d4a093eeae83d9726a20295498timer()", 1000);
}
 }
jQuery(document).ready(function(){
var holder_stuff = '<span class="number"></span><span class="over"></span><span class="title"></span>';
var holder_days = jQuery("<span>").addClass("days").addClass("item").html(holder_stuff);
var holder_hour = jQuery("<span>").addClass("hour").addClass("item").html(holder_stuff);
var holder_mins = jQuery("<span>").addClass("mins").addClass("item").html(holder_stuff);
var holder_secs = jQuery("<span>").addClass("secs").addClass("item").html(holder_stuff);
jQuery("#951124d4a093eeae83d9726a20295498").html("").append(holder_days).append(holder_hour).append(holder_mins).append(holder_secs);
count951124d4a093eeae83d9726a20295498timer();
});
</script>
4

1 回答 1

1

这一行是错误的:

var dateFuture = new Date(<?php print date("Y", $strTime) ?>,<?php print (date("m", $strTime)-1) ?>,<?php print date("d", $strTime) ?>,<?php print date("h", $strTime) ?>,<?php print date("i", $strTime) ?>,<?php print date("s", $strTime) ?>);

你想不想

<?php print date("h", $strTime) ?>

你要

<?php print date("H", $strTime) ?>

换句话说,您想要 0-23 小时,而不是 1-12 小时。

见: http: //php.net/manual/en/function.date.php

于 2014-10-23T18:11:19.140 回答