Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save a-am/2e8ff9d9ed4c03be75dcbe9cd8209e66 to your computer and use it in GitHub Desktop.

Select an option

Save a-am/2e8ff9d9ed4c03be75dcbe9cd8209e66 to your computer and use it in GitHub Desktop.

Revisions

  1. @phpmypython phpmypython created this gist Nov 14, 2015.
    76 changes: 76 additions & 0 deletions phpDateTimetoMomentFormat.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    (function (m) {
    /*
    * PHP => moment.js
    * Will take a php date format and convert it into a JS format for moment
    * http://www.php.net/manual/en/function.date.php
    * http://momentjs.com/docs/#/displaying/format/
    */
    var formatMap = {
    d: 'DD',
    D: 'ddd',
    j: 'D',
    l: 'dddd',
    N: 'E',
    S: function () {
    return '[' + this.format('Do').replace(/\d*/g, '') + ']';
    },
    w: 'd',
    z: function () {
    return this.format('DDD') - 1;
    },
    W: 'W',
    F: 'MMMM',
    m: 'MM',
    M: 'MMM',
    n: 'M',
    t: function () {
    return this.daysInMonth();
    },
    L: function () {
    return this.isLeapYear() ? 1 : 0;
    },
    o: 'GGGG',
    Y: 'YYYY',
    y: 'YY',
    a: 'a',
    A: 'A',
    B: function () {
    var thisUTC = this.clone().utc(),
    // Shamelessly stolen from http://javascript.about.com/library/blswatch.htm
    swatch = ((thisUTC.hours() + 1) % 24) + (thisUTC.minutes() / 60) + (thisUTC.seconds() / 3600);
    return Math.floor(swatch * 1000 / 24);
    },
    g: 'h',
    G: 'H',
    h: 'hh',
    H: 'HH',
    i: 'mm',
    s: 'ss',
    u: '[u]', // not sure if moment has this
    e: '[e]', // moment does not have this
    I: function () {
    return this.isDST() ? 1 : 0;
    },
    O: 'ZZ',
    P: 'Z',
    T: '[T]', // deprecated in moment
    Z: function () {
    return parseInt(this.format('ZZ'), 10) * 36;
    },
    c: 'YYYY-MM-DD[T]HH:mm:ssZ',
    r: 'ddd, DD MMM YYYY HH:mm:ss ZZ',
    U: 'X'
    },
    formatEx = /[dDjlNSwzWFmMntLoYyaABgGhHisueIOPTZcrU]/g;

    moment.fn.formatPHP = function (format) {
    var that = this;

    return this.format(format.replace(formatEx, function (phpStr) {
    return typeof formatMap[phpStr] === 'function' ? formatMap[phpStr].call(that) : formatMap[phpStr];
    }));
    };
    }(moment));
    //Usage Example
    //You can pass whatever date into the moment call and any php date format into the formatter.
    moment(new Date()).formatPHP("examplePHPFormat");