为什么此javascript使用getTime函数显示负数? - javascript

我无法弄清楚,希望有一个简单的解决方案,但看不到该脚本在哪里失败。该脚本是wp插件的一部分,但是如果失败,则没有wp代码。

它所做的是算到结束日期并在屏幕上打印出来。该行:

数量=(dateFuture.getTime()-dateNow.getTime()+ 5);

显示负数,所以不可能,将来的日期是“ 2014-10-23 23:00:00”,而我所在国家/地区的当前日期和时间是“ 2014-10-23 20:00:00”不能是<如果dateFuture-dateNow,则为0。代码在实际日期(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>

javascript大神给出的解决方案

这行是错误的:

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

PHP stringID无法从回显中读入onclick - javascript

我有一个音频播放器,可以在HTML中正常工作,但是当我从PHP回显调用时,似乎没有得到div ID的名称,因此它无法播放我的音频。这是我的代码:// Show audio if ($sObj->get('audio') != null) { $sAudio = $sObj->get('audio'); $a…

如何添加5分钟的JavaScript倒数计时器来提交表单 - javascript

我想添加5分钟的倒数计时器,将其提交到我的php页面。 <form name="myfm" id="myfm" method="post" action="Quiz.php"> <table width=100%> <tr> <td wi…

在php中单击按钮添加表行并设置字段值 - javascript

我有一个表单,必须在单击按钮时添加表行。这是我必须动态添加的表行:(addProdToGroup.php)<tr style="text-align: center;" id="products"> <td><?php $j ?></td> <td><s…

如果复选框切换复选框已选中,则在切换div中输入必填字段 - javascript

我使用脚本用JavaScript切换了一些div。如果要选中复选框以显示toogle div,我想在toogle div中设置一些“必填”输入字段。有人能弄清楚吗?那是工作吗?function show(id) { if(document.getElementById) { var mydiv = document.getElementById(id); m…

提交表单后显示模式对话框 - php

提交下载文件后,我有一张表格。我要自动而不是自动下载文件..以显示模态对话框并显示下载链接。<form name="softwareform" id="softwareform" action="../downloadlink.php" method="POST" alig…