Ren*_*ran 11 javascript php momentjs
我有一个php dateformats的配置
'dateFormat' => 'd.m.Y',
'timeFormat' => 'H:i',
'dateTimeFormat' => 'd.m.Y H:i',
Run Code Online (Sandbox Code Playgroud)
但对于datetimepicker我需要moment.js格式化(http://momentjs.com/docs/#/displaying/format/),如下所示:
DD.MM.YYYY
HH:mm
DD.MM.YYYY HH:mm
Run Code Online (Sandbox Code Playgroud)
这将是没有问题的,我来代替d用DD和m用MM,但我想知道如果没有人之前已建成的东西做到这一点.
Ren*_*ran 30
所以我写了一个litte helper函数将php dateformats转换为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;
}
Run Code Online (Sandbox Code Playgroud)
小智 9
我知道这已经很老了,但我刚刚遇到了这个问题。非常感谢 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);
}
Run Code Online (Sandbox Code Playgroud)
注意:A,a并且W值得保留,以防您像 Samuel Georges 评论中那样转换转义字符。
NB2:u实际上是微秒,v(自 PHP 7.0.0 起)是毫秒。
| 归档时间: |
|
| 查看次数: |
5794 次 |
| 最近记录: |