开发者

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.

timestamp is created with php

<? php echo hours ();?>

And I wan开发者_运维问答t the script to count down in real time with JavaScript.

Example 2 days, 3:15:39

Hope someone can help me a little:)


First you have some PHP errors. It should be e.g.

<?php echo time(); ?>

I'm using a timestamp in JavaScript for the ease of showing an example.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜