Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php dateformat to moment js format

I have got a config for php dateformats

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

But for the datetimepicker i need moment.js formatting (http://momentjs.com/docs/#/displaying/format/) that will look like so:

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

This would be no problem for me to replace d with DD and m with MM but i was wondering if nobody before has built something to do this.

like image 844
Rene Vorndran Avatar asked May 12 '15 09:05

Rene Vorndran


2 Answers

I know this is quite old but I just bumped into this question. Many thanks Rene Vorndran for the initial mapping. I wanted to add a comment to your Answer but couldn't (not enough points), so I'm writing this answer just to complete a few mappings, mostly completing Rene Vorndran's Answer and Samuel Georges' comment to come to this:

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

NB: A, a and W are worth being kept in case you converts the escaped characters as in Samuel Georges comments.

NB2: u is in fact microseconds and v (since PHP 7.0.0) is milliseconds.

like image 175
Julix Avatar answered Sep 28 '22 09:09

Julix


So i wrote a litte helper function to convert the php dateformats into the format needed for 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;
}
like image 36
Rene Vorndran Avatar answered Sep 28 '22 09:09

Rene Vorndran