使用jQuery的倒数计时器与MySQL的日期时间? [英] Using jquery countdown timer with mysql datetime?
问题描述
我知道这个问题已经被问过很多次了,因为我在Google以及stackoverflow上都发现了一些问题.
I know this question has been asked many times as I have found a few on google and also on stackoverflow.
但他们都没有解释如何在我的php中格式化日期时间,因此它可以与jquery倒数计时器结合使用.所以我要在这里问这个,希望有人能帮我弄清楚这一点.
but none of them explained how to format my datetime in my php so it works in combination with jquery countdown timer. so I am going to ask it here in a hope i get someone shed a light on this for me.
基本上我想做的是创建一个倒数计时器,该倒数计时器将与mysql datetime一起使用.
Basically what i am trying to do is to create a countdown timer which will work with mysql datetime.
datetime存储在mysql中,因此所需要做的就是在我的php中获取正确的格式,以便倒数计时器可以使用它.
the datetime is stored in mysql so All need to do is to get the correct format in my php so the countdown timer could work with it.
我正在使用此插件: http://keith-wood.name/countdown.html
这是我到目前为止所拥有的:
and here is what i have so far:
PHP格式:
$end_date = date("m d Y H:i:s T", strtotime($row["end_date"]));
jQuery/Javascript代码:
Jquery/Javascript code:
<script type="text/javascript">
$(function(){
var countdown = $('#countdown'),
ts = new Date(<?php echo $end_date * 1000; ?>),
finished = true;
if((new Date()) > ts)
{
finished = false;
}
$('#defaultCountdown').countdown({
timestamp : ts,
callback : function(days, hours, minutes, seconds)
{
var message = "";
message += days + " days, ";
message += hours + " hours, ";
message += minutes + " minutes, ";
message += seconds + " seconds ";
message = (finished ? "Countdown finished" : "left untill the New Year");
countdown.html(message);
}
});
});
</script>
当我运行这段代码时,我得到的只是0 hours, 0 minutes, 0 seconds.
when i run this code, all i get is 0 hours, 0 minutes, 0 seconds.
我只能怀疑问题出在我的php部分中格式化日期时间!
I can only suspect that the issue is from formatting the datetime in my php section!
或者我还想念其他东西吗?
or am i missing something else as well?
好吧,我设法将代码缩至:
<script type="text/javascript">
$(document).ready(function () {
$('#defaultCountdown').countdown({
until: new Date(<?php echo $end_date; ?>),
compact: true
});
});
</script>
并将php更改为此:
$end_date = date("Y, n, j, G, i, s", strtotime($row["end_date"]));
但是,coutdown计时器中显示的时间是错误的(关闭).
However, the time shown in the coutdown timer is wrong (way off).
$end_date
是:mysql datetime中的September 22 2013 23:30:00 GMT
the $end_date
is: September 22 2013 23:30:00 GMT
in mysql datetime
但是jquery的倒数计时器显示为:
but the jquery countdown timer is showing:
34d 06:21:48
2013, 9, 22, 23, 30, 00
34天零6个小时等等绝对是错误的!
34days and 6 hours blah blah is absolutely wrong!
我现在在做什么错了?
推荐答案
JavaScript Date
对象的构造如下:
The JavaScript Date
object is constructed as follows:
Date(year, month, day, hours, minutes, seconds, milliseconds)
这意味着您可能应该按照以下方式进行操作:
That means you probably should be doing something along these lines:
$end_date = date("Y, n, j, G, i, s", strtotime($row["end_date"]));
来源:
- JavaScript Date-object
- PHP date-function
此外,我似乎在 jQuery Countdown 手册中发现了这个问题:>
In addition, I seem to have found the problem in the jQuery Countdown manual:
关于日期的注释-JavaScript Date构造函数期望年份, 月和日作为参数.但是,月份的范围是0到11. 明确指出预定的日期(3个月表示3月 还是四月?)我将月份指定为1到12,然后手动减去 1.因此,以下表示2010年12月25日.
A note on Date - the JavaScript Date constructor expects the year, month, and day as parameters. However, the month ranges from 0 to 11. To make explicit what date is intended (does a month of 3 mean March or April?) I specify the month from 1 to 12 and manually subtract the 1. Thus the following denotes 25 December, 2010.
因此,您必须拆分字符串,从月份中减去1,然后重新构建...
So, you'd have to split the string, substract 1 from the month and rebuild...
$tmp_date = explode(', ', $end_date);
$tmp_date[1] = $tmp_date[1] - 1;
$end_date = implode(', ', $tmp_date);
链接到 jsFiddle
这篇关于使用jQuery的倒数计时器与MySQL的日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!