开发者

How to convert a time format that I got from PHP to a format that jQuery countdown accepts

I have this kind of time format, it is stored in my database.. And I want to convert it to a format that jQuery countdown accepts.. I think jQuery countdown accepts this kind of format

Sun Jan 01 2012 00:00:00 GMT+0800 (Malay Peninsula Standard Tim开发者_JAVA技巧e)

But the problem is, my time format is like this:

2011-03-29 00:01:03

In order for jQuery countdown to make a countdown, I need to convert that to that long format.. How to do it?

Here's the website of jQuery countdown


Split your format then create a new Date() and pass it to the countdown constructor:

$dateAndTime = '2011-03-29 00:01:03'.split(" ");
$date = $dateAndTime[0].split("-");
$time = $dateAndTime[1].split(":");
// Date(year, month, day, hours, minutes, seconds)

$(selector).countdown({
     since: new Date( $date[0], (intval($date[1]) - 1), $date[2], $time[0], $time[1], $time[2])
});


You don't need it in that long format, that is what is just the format that is output when you try to print a Javascript Date object.

You need to create a Javascript Date object

The native way to do that is like so:

var date = new Date([year], [month], [day]);

Note: the month is zero indexed. i.e. January is 0, February is 1, December is 11.

So if you were spitting this out using php.

$date = new DateTime('2011-03-29 00:01:03');
printf('var date = new Date(%d, %d, %d);', 
    $date->format('Y'),
    $date->format('n') - 1,
    $date->format('j'),
    $date->format('H'),
    $date->format('i'),
    $date->format('s')
);

Alternatively you could pass it using json:

json_encode(array(
    'year' => $date->format('Y'),
    'month' => $date->format('n') - 1,
    'day' => $date->format('j')
    'hour' => $date->format('H'),
    'minute' => $date->format('i'),
    'second' => $date->format('s')
));

then create the Date with Javascript:

var date = new Date(json.year, json.month, json.day, 
    json.hour, json.minute, json.second);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜