JavaScript 倒计时到 MySQL 日期时间格式

发布于 2024-09-25 05:23:52 字数 158 浏览 3 评论 0原文

我有一个来自 MySQL 数据库的 datetime 格式的日期,例如 2010-09-24 11:30:12。我需要一种方法来显示到该日期的小时:分钟倒计时。

我对 JavaScript 中的日期不太熟悉,因此我们将不胜感激。谢谢。

I have a date that comes from a MySQL database in the datetime format, something like 2010-09-24 11:30:12. And I need a way to show a countdown of hours:mins to that date.

I'm not very familiar with dates in JavaScript so any help would be apreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我偏爱纯白色 2024-10-02 05:23:52

您可能需要使用 < MySQL 中的 code>UNIX_TIMESTAMP() 函数以 Unix 时间格式< /a>(自 1970 年 1 月 1 日以来的秒数)。假设我们的目标日期是 '2011-01-01 00:00:00' (实际上,您的表中可能有一个字段,而不是常量):

SELECT UNIX_TIMESTAMP('2011-01-01 00:00:00') AS timestamp;
+---------------+
| timestamp     |
+---------------+
| 1293836400    |
+---------------+
1 row in set (0.00 sec)

然后我们可以使用 < a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime" rel="nofollow">getTime() 方法 < JavaScript 中的 code>Date 对象用于计算当前时间与目标时间之间的秒数。一旦我们有了秒数,我们就可以轻松计算出小时、分钟和天数:

var target = 1293836400;    // We got this from MySQL
var now = new Date();       // The current tume

var seconds_remaining = target - (now.getTime() / 1000).toFixed(0);
var minutes_remaining = seconds_remaining / 60;
var hours_remaining = minutes_remaining / 60;
var days_remaining = hours_remaining / 24;

alert(seconds_remaining + ' seconds');     // 8422281 seconds
alert(minutes_remaining + ' minutes');     // 140371.35 minutes
alert(hours_remaining + ' hours');         // 2339.5225 hours
alert(days_remaining + ' days');           // 97.48010416666666 days

You may want to use the UNIX_TIMESTAMP() function in MySQL to return your date in Unix Time Format (the number of seconds since January 1, 1970). Let's say our target date is '2011-01-01 00:00:00' (in reality you would probably have a field from your table, instead of a constant):

SELECT UNIX_TIMESTAMP('2011-01-01 00:00:00') AS timestamp;
+---------------+
| timestamp     |
+---------------+
| 1293836400    |
+---------------+
1 row in set (0.00 sec)

Then we can use the getTime() method of the Date object in JavaScript to calculate the number of seconds between the current time and the target time. Once we have the number of seconds, we can easily calculate the hours, minutes and days:

var target = 1293836400;    // We got this from MySQL
var now = new Date();       // The current tume

var seconds_remaining = target - (now.getTime() / 1000).toFixed(0);
var minutes_remaining = seconds_remaining / 60;
var hours_remaining = minutes_remaining / 60;
var days_remaining = hours_remaining / 24;

alert(seconds_remaining + ' seconds');     // 8422281 seconds
alert(minutes_remaining + ' minutes');     // 140371.35 minutes
alert(hours_remaining + ' hours');         // 2339.5225 hours
alert(days_remaining + ' days');           // 97.48010416666666 days
只怪假的太真实 2024-10-02 05:23:52

您可以使用jquery插件 http://keith-wood.name/countdown.html

只需传递您的mysql日期和时间到js作为js变量

语法是

$(selector).countdown({until: liftoffTime, format: 'HMS'})

这里的js代码

 function mysqlTimeStampToDate(timestamp) {
    //function parses mysql datetime string and returns javascript Date object
    //input has to be in this format: 2010-09-24 11:30:12
    var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
    var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
    return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
  }
$('#selector').countdown({until: mysqlTimeStampToDate(<? echo "2010-09-24 11:30:12" ?>), format: 'HMS'});

You can use jquery plugin http://keith-wood.name/countdown.html

Just pass your mysql date and time to js as js variable

syntax is

$(selector).countdown({until: liftoffTime, format: 'HMS'})

js code here

 function mysqlTimeStampToDate(timestamp) {
    //function parses mysql datetime string and returns javascript Date object
    //input has to be in this format: 2010-09-24 11:30:12
    var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
    var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
    return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
  }
$('#selector').countdown({until: mysqlTimeStampToDate(<? echo "2010-09-24 11:30:12" ?>), format: 'HMS'});
若水微香 2024-10-02 05:23:52

有一个 Date.parse() 方法可能有效。

There is a Date.parse() method that might work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文