为什么这个javascript用getTime函数显示负数?

时间:2014-10-23 17:53:39

标签: javascript php wordpress date gettime

我无法弄清楚,希望有一个简单的解决方案,但我无法看到此脚本失败的地方。这个脚本是wp插件的一部分,但在失败的地方没有wp代码。

它的作用是计入结束日期并在屏幕上打印。这一行:

amount =(dateFuture.getTime() - dateNow.getTime()+ 5);

显示负数,因此不可能,未来日期是" 2014-10-23 23:00:00"我国目前的日期和时间是" 2014-10-23 20:00:00"不能< 0如果dateFuture - dateNow。代码在实际日期(2014-10-23)时失败,但没有小时。即实际日期总是显示日:0,小时:0,分钟:0,秒: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>

1 个答案:

答案 0 :(得分: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