dongshen6060 2011-03-28 10:54
浏览 54
已采纳

如何将我从PHP获得的时间格式转换为jQuery倒计时接受的格式

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 Time)

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

  • 写回答

2条回答 默认 最新

  • drtppp75155 2011-03-28 11:06
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应