javascript和php中的时间戳记倒计时 [英] timestamp countdown in javascript and php

查看:62
本文介绍了javascript和php中的时间戳记倒计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助来编写一个脚本,该脚本可以从unix时间戳开始倒数几天,几小时,几分钟和几秒钟.

I need some help to code a script that counts down the days, hours, minutes and seconds from a unix timestamp.

时间戳是使用php创建的

timestamp is created with php

<? php echo hours ();?>

我希望脚本可以使用JavaScript实时倒计时.

And I want the script to count down in real time with JavaScript.

示例 2天,3:15:39

Example 2 days, 3:15:39

希望有人可以帮助我一点:)

Hope someone can help me a little:)

推荐答案

首先,您有一些PHP错误.应该是

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

<?php echo time(); ?>

为了方便显示示例,我在JavaScript中使用了时间戳.

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

这篇关于javascript和php中的时间戳记倒计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆