【问题标题】:How to convert a time format that I got from PHP to a format that jQuery countdown accepts如何将我从 PHP 获得的时间格式转换为 jQuery 倒计时接受的格式
【发布时间】:2011-07-24 09:31:47
【问题描述】:

我有这种时间格式,它存储在我的数据库中。我想将其转换为 jQuery countdown 接受的格式。我认为 jQuery countdown 接受这种格式

Sun Jan 01 2012 00:00:00 GMT+0800 (Malay Peninsula Standard Time)

但问题是,我的时间格式是这样的:

2011-03-29 00:01:03

为了让jQuery倒计时进行倒计时,我需要将其转换为长格式..怎么做?

这里是jQuerycountdown的网址

【问题讨论】:

标签: php javascript jquery date time


【解决方案1】:

拆分格式,然后创建一个新的 Date() 并将其传递给倒计时构造函数:

$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])
});

【讨论】:

    【解决方案2】:

    您不需要那种长格式,这正是您尝试打印 Javascript Date 对象时输出的格式。

    你需要创建一个 Javascript Date object

    这样做的原生方式是这样的:

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

    注意:月份索引为零。即一月是 0,二月是 1,十二月是 11。

    因此,如果您使用 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')
    );
    

    您也可以使用 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')
    ));
    

    然后用 Javascript 创建日期:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 2016-01-02
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      相关资源
      最近更新 更多