简体   繁体   English

javascript和php中的时间戳记倒计时

[英]timestamp countdown in javascript and php

I need some help to code a script that counts down the days, hours, minutes and seconds from a unix timestamp. 我需要一些帮助来编写一个脚本,该脚本可以从unix时间戳记下几天,几小时,几分钟和几秒钟。

timestamp is created with php 时间戳是用php创建的

<? php echo hours ();?>

And I want the script to count down in real time with JavaScript. 我希望脚本能够使用JavaScript实时倒计时。

Example 2 days, 3:15:39 示例 2天3:15:39

Hope someone can help me a little:) 希望有人可以帮助我一点:)

First you have some PHP errors. 首先,您有一些PHP错误。 It should be eg 应该是

<?php echo time(); ?>

I'm using a timestamp in JavaScript for the ease of showing an example. 为了方便显示示例,我在JavaScript中使用了时间戳。

http://jsfiddle.net/pimvdb/AvXd3/ http://jsfiddle.net/pimvdb/AvXd3/

// the difference timestamp
var timestamp = (Date.now() + 1000 * 60 * 60 * 24 * 3) - Date.now();

timestamp /= 1000; // from ms to seconds

function component(x, v) {
    return Math.floor(x / v);
}

var $div = $('div');

setInterval(function() { // execute code each second

    timestamp--; // decrement timestamp with one second each second

    var days    = component(timestamp, 24 * 60 * 60),      // calculate days from timestamp
        hours   = component(timestamp,      60 * 60) % 24, // hours
        minutes = component(timestamp,           60) % 60, // minutes
        seconds = component(timestamp,            1) % 60; // seconds

    $div.html(days + " days, " + hours + ":" + minutes + ":" + seconds); // display

}, 1000); // interval each second = 1000 ms

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM