php dateformat到时刻js格式

时间:2015-05-12 09:18:09

标签: javascript php momentjs

我有一个php dateformats的配置

'dateFormat'        => 'd.m.Y',
'timeFormat'        => 'H:i',
'dateTimeFormat'    => 'd.m.Y H:i',

但对于datetimepicker我需要moment.js格式化(http://momentjs.com/docs/#/displaying/format/),如下所示:

DD.MM.YYYY
HH:mm
DD.MM.YYYY HH:mm

对于d替换DDm替换MM,这对我来说没有问题,但我想知道之前是否有人建立了这样做。< / p>

3 个答案:

答案 0 :(得分:28)

所以我写了一个小函数帮助函数将php日期格式转换为moment.js所需的格式

function convertPHPToMomentFormat($format)
{
    $replacements = [
        'd' => 'DD',
        'D' => 'ddd',
        'j' => 'D',
        'l' => 'dddd',
        'N' => 'E',
        'S' => 'o',
        'w' => 'e',
        'z' => 'DDD',
        'W' => 'W',
        'F' => 'MMMM',
        'm' => 'MM',
        'M' => 'MMM',
        'n' => 'M',
        't' => '', // no equivalent
        'L' => '', // no equivalent
        'o' => 'YYYY',
        'Y' => 'YYYY',
        'y' => 'YY',
        'a' => 'a',
        'A' => 'A',
        'B' => '', // no equivalent
        'g' => 'h',
        'G' => 'H',
        'h' => 'hh',
        'H' => 'HH',
        'i' => 'mm',
        's' => 'ss',
        'u' => 'SSS',
        'e' => 'zz', // deprecated since version 1.6.0 of moment.js
        'I' => '', // no equivalent
        'O' => '', // no equivalent
        'P' => '', // no equivalent
        'T' => '', // no equivalent
        'Z' => '', // no equivalent
        'c' => '', // no equivalent
        'r' => '', // no equivalent
        'U' => 'X',
    ];
    $momentFormat = strtr($format, $replacements);
    return $momentFormat;
}

答案 1 :(得分:0)

如果有人需要将Moment.js格式转换为PHP,请使用Rene Vorndran的代码编写以下函数:

function convertMomentFormatToPhp($format)
{
    $replacements = [
        'DD'   => 'd',
        'ddd'  => 'D',
        'D'    => 'j',
        'dddd' => 'l',
        'E'    => 'N',
        'o'    => 'S',
        'e'    => 'w',
        'DDD'  => 'z',
        'W'    => 'W',
        'MMMM' => 'F',
        'MM'   => 'm',
        'MMM'  => 'M',
        'M'    => 'n',
        'YYYY' => 'Y',
        'YY'   => 'y',
        'a'    => 'a',
        'A'    => 'A',
        'h'    => 'g',
        'H'    => 'G',
        'hh'   => 'h',
        'HH'   => 'H',
        'mm'   => 'i',
        'ss'   => 's',
        'SSS'  => 'u',
        'zz'   => 'e',
        'X'    => 'U',
    ];

    $phpFormat = strtr($format, $replacements);

    return $phpFormat;
}

答案 2 :(得分:0)

我知道这已经很老了,但我碰到了这个问题。非常感谢Rene Vorndran提供的初始映射。我想在您的答案中添加评论,但不能(分数不足),所以我写这个答案只是为了完成一些映射,主要是完成里内·旺德兰的答案和塞缪尔·乔治的评论:

/**
 * Converts php DateTime format to Javascript Moment format.
 * @param string $phpFormat
 * @return string
 */
public function convertPhpToJsMomentFormat(string $phpFormat): string
{
    $replacements = [
        'A' => 'A',      // for the sake of escaping below
        'a' => 'a',      // for the sake of escaping below
        'B' => '',       // Swatch internet time (.beats), no equivalent
        'c' => 'YYYY-MM-DD[T]HH:mm:ssZ', // ISO 8601
        'D' => 'ddd',
        'd' => 'DD',
        'e' => 'zz',     // deprecated since version 1.6.0 of moment.js
        'F' => 'MMMM',
        'G' => 'H',
        'g' => 'h',
        'H' => 'HH',
        'h' => 'hh',
        'I' => '',       // Daylight Saving Time? => moment().isDST();
        'i' => 'mm',
        'j' => 'D',
        'L' => '',       // Leap year? => moment().isLeapYear();
        'l' => 'dddd',
        'M' => 'MMM',
        'm' => 'MM',
        'N' => 'E',
        'n' => 'M',
        'O' => 'ZZ',
        'o' => 'YYYY',
        'P' => 'Z',
        'r' => 'ddd, DD MMM YYYY HH:mm:ss ZZ', // RFC 2822
        'S' => 'o',
        's' => 'ss',
        'T' => 'z',      // deprecated since version 1.6.0 of moment.js
        't' => '',       // days in the month => moment().daysInMonth();
        'U' => 'X',
        'u' => 'SSSSSS', // microseconds
        'v' => 'SSS',    // milliseconds (from PHP 7.0.0)
        'W' => 'W',      // for the sake of escaping below
        'w' => 'e',
        'Y' => 'YYYY',
        'y' => 'YY',
        'Z' => '',       // time zone offset in minutes => moment().zone();
        'z' => 'DDD',
    ];

    // Converts escaped characters.
    foreach ($replacements as $from => $to) {
        $replacements['\\' . $from] = '[' . $from . ']';
    }

    return strtr($phpFormat, $replacements);
}

注意:AaW值得保留,以防您像Samuel Georges注释中那样转换转义字符。

NB2:u实际上是毫秒,而v(自PHP 7.0.0起)是毫秒。