PHP日期格式转换为Moment.js格式

15

我有一个 PHP 日期格式的配置。

'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替换为DD,将m替换为MM不是问题,但我想知道之前是否已经有人构建过类似的工具。


我想配置datetimepicker中时间的显示方式,这需要使用略有不同的字符串。如果无需两次配置,则非常好,首先是为了在php中显示时间,第二次则为了在JavaScript中使用相同格式但以其他方式显示。 - Rene Vorndran
它们是同样的格式但又不同,这不就是另一种格式的定义吗?但是,没有什么神奇的方法可以检测所需的格式;你可以编写一个函数来自行转换你的 PHP 格式和 JS 格式(尽管并不存在像你想象中的通用 JS 格式)。 - Erik S
我知道没有“通用”的JS格式,但是moment.js(datetimepicker使用的)有这个格式http://momentjs.com/docs/#/displaying/format/来告诉选择器接受什么。 - Rene Vorndran
那我建议编写一个小函数来完成这个转换 :) - Erik S
$('.datepicker').datetimepicker({ format: 'DD.MM.YYYY' }); $('.timepicker').datetimepicker({ format: 'HH:mm' }); $('.datetimepicker').datetimepicker({ format: 'DD.MM.YYYY HH:mm' }); 这是我创建选择器的方式。我希望它们显示与我在配置中为 PHP 定义的相同格式,但它们需要这种其他格式,因此自动转换会很好。 - Rene Vorndran
显示剩余2条评论
3个回答

42
所以我写了一个小助手函数,用于将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;
}

您还可以添加一个额外的步骤来支持 PHP 中的转义。在 $replacements 数组定义之后直接添加以下代码:foreach ($replacements as $from => $to) { $replacements['\\'.$from] = '['.$from.']'; } - Samuel Georges
2
你应该将其制作为一个Composer包。谢谢你提供这个代码片段。 - Josef Sábl

12

我知道这已经很旧了,但我刚好遇到了这个问题。非常感谢Rene Vorndran的初始映射。我想添加一个评论到你的答案,但是不能(没有足够的积分),所以我写下这个答案只是为了补充一些映射,大多数是为了完成Rene Vorndran的答案和Samuel Georges的评论,得出以下结论:

/**
 * 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);
}

注意:如果您按照Samuel Georges的评论将转义字符转换,则应保留AaW
注意2:实际上,u代表微秒,v(自PHP 7.0.0起)代表毫秒。

3
如果有人需要将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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接