php dateformat转moment js格式
php dateformat to moment js format
我有 php 日期格式的配置
'dateFormat' => 'd.m.Y',
'timeFormat' => 'H:i',
'dateTimeFormat' => 'd.m.Y H:i',
但是对于日期时间选择器,我需要 moment.js 格式 (http://momentjs.com/docs/#/displaying/format/),它看起来像这样:
DD.MM.YYYY
HH:mm
DD.MM.YYYY HH:mm
这对我来说用 DD
替换 d
和 MM
替换 m
没问题,但我想知道之前是否没有人构建过这样做的东西.
所以我写了一个小辅助函数来将 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;
}
以防有人需要将 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;
}
我知道这已经很老了,但我只是碰到了这个问题。非常感谢 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);
}
注意:A
、a
和 W
值得保留,以防您像 Samuel Georges 评论中那样转换转义字符。
NB2:u
实际上是微秒,v
(自 PHP 7.0.0 起)是毫秒。
我有 php 日期格式的配置
'dateFormat' => 'd.m.Y',
'timeFormat' => 'H:i',
'dateTimeFormat' => 'd.m.Y H:i',
但是对于日期时间选择器,我需要 moment.js 格式 (http://momentjs.com/docs/#/displaying/format/),它看起来像这样:
DD.MM.YYYY
HH:mm
DD.MM.YYYY HH:mm
这对我来说用 DD
替换 d
和 MM
替换 m
没问题,但我想知道之前是否没有人构建过这样做的东西.
所以我写了一个小辅助函数来将 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;
}
以防有人需要将 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;
}
我知道这已经很老了,但我只是碰到了这个问题。非常感谢 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);
}
注意:A
、a
和 W
值得保留,以防您像 Samuel Georges 评论中那样转换转义字符。
NB2:u
实际上是微秒,v
(自 PHP 7.0.0 起)是毫秒。