Krajee

PHP Date Formatter

Thankful to Krajee! BUY A COFFEEor to get more out of us.

A Javascript datetime library that allows you to manipulate date/times using PHP date-time formats. This library was built with an intention to read and write date/timestamps to the database easily when working with PHP server code. Use cases for this library would involve reading and saving a timestamp to database in one format, but displaying it on client or html forms in another format. Maintaining a consistent PHP Date time format for both server side and client side validation should help in building extensible applications with various PHP frameworks easily. View a complete demo.

Tip

Not seeing the updated content on this page! Hard refresh your browser to clean cache for this page (e.g. SHIFT-F5 on Windows Chrome)

The DateFormatter library offers these enhanced features:

  • Parse date/time strings or a Date object, and convert it into Javascript Date Object by passing any of the PHP DateTime formats.

  • Automatically guess date/time strings, even if it does not exactly match the format, and convert it into Javascript Date Object.

  • Read date/time strings or a Date object, and format it as per a PHP DateTime format.

  • With release v1.3.2 the library has been converted to use pure javacript code without dependency on jQuery or other third party JS library.

No third party pre-requisites for this library. Some methods (like timezone string detection) depend on browser support (generally supported by most modern desktop browsers).

The php-date-formatter plugin can be installed automatically or manually using one of these options:

Bower Package Manager


Installation via bower package manager is as simple as running:

$ bower install php-date-formatter

Node Package Manager


Installation via NPM is as simple as running:

$ npm install php-date-formatter

Composer Package Manager


You can install php-date-formatter via composer package manager. Either run:

$ php composer.phar require kartik-v/php-date-formatter "dev-master"

or add:

"kartik-v/php-date-formatter": "dev-master"

to your composer.json file

Manual Install


You can also manually install the plugin easily to your project. Just download the source ZIP or TAR ball and extract the plugin asset files and folders into your project.

Load Client Assets

You must first load the following assets in your header.

<script src="path/to/js/php-date-formatter.min.js" type="text/javascript"></script>

Using DateFormatter

Once loaded, you can directly use the DateFormatter methods in your application.
var d1 = DateFormatter.parseDate('23-Sep-2013 09:24:12', 'd-M-Y H:i:s');

View a complete demo.

You can control the following options/settings for DateFormatter.

dateSettings

array, This property allows you to configure the terms used for short/long days and months, the meridiem, and the ordinal suffix (for day of the month - works best with j). This is defaulted to:

{
    longDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
    shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
    shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    longMonths: ['January', 'February', 'March', 'April', 'May', 'June',
        'July', 'August', 'September', 'October', 'November', 'December'],
    meridiem: ['AM', 'PM'],
    ordinal: function (number) {
        var n = number % 10, suffixes = {1: 'st', 2: 'nd', 3: 'rd'};
        return Math.floor(number % 100 / 10) === 1 || !suffixes[n] ? 'th' : suffixes[n];
    }
}

You can override these settings for your language, as shown in the example below:

var fmt = new DateFormatter({
    dateSettings: {
        longMonths: ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 
            'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre']
    }
});

separators

Regex, the separators that are possible in the date string. This defaults to /[ \-+\/\.T:@]/g.

validParts

Regex, the valid format patterns that are possible in the date string. This defaults to /[dDjlNSwzWFmMntLoYyaABgGhHisueTIOPZcrU]/g.

intParts

Regex, the integer format patterns for which the ordinal suffix will automatically be calculated. This defaults to /[djwNzmnyYhHgGis]/g.

tzParts

Regex, the timezone patterns to scan for in the date string. This defaults to /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g.

tzClip

Regex, the separator patterns for clipping the timezone string. This defaults to /[^-+\dA-Z]/g.

The php-date-formatter library supports these methods:

parseDate

This method allows you to convert any date string or date object to a javascript Date object for a specific PHP date format. This method takes in the following properties:

  • vDate: Input date. Must be string or a javascript Date object.

  • vFormat: The PHP DateTime format, used for parsing the date.

Example:

var fmt = new DateFormatter();
var d1 = fmt.parseDate('23-Sep-2013 09:24:12', 'd-M-Y H:i:s');
// d1 will return Javascript date object for your timezone
alert(d1); // will return: Mon Sep 23 2013 09:24:12 GMT+0530 (India Standard Time)

guessDate

This method allows you to intelligently guess the date by closely matching the specific format. You can guess a date for any date string or date object to return a javascript Date object. This method takes in the following properties:

  • vDate: Input date. Must be string or a javascript Date object.

  • vFormat: The PHP DateTime format, used for parsing the date.

Example:

// note the passed string and the PHP date-time format differ
var fmt = new DateFormatter();
var d1 = fmt.guessDate('23-09-13 09:24:12', 'd-M-Y H:i:s'); 
// d1 will again return Javascript date object for your timezone
alert(d1); // will return: Mon Sep 23 2013 09:24:12 GMT+0530 (India Standard Time)

formatDate

This method allows you to return a formatted date string using a specific PHP datetime format. You can format a date for any date string or date object to return a javascript Date object. This method takes in the following properties:

  • vDate: Input date. Must be string or a javascript Date object.

  • vFormat: The PHP DateTime format, used for parsing the date.

var d1 = new Date();
var fmt = new DateFormatter();
var d2 = fmt.formatDate(d1, 'd-M-Y H:i:s');
alert(d2); // will return: 23-Sep-2013 09:24:12

The library has been implemented as extensions in the following frameworks

Sl.FrameworkExtension SourceDeveloped ByExtension Demo
1.Yii Framework 2.0Yii2 DateControlKrajee Yii2 DateControl

Do you know any other framework extension using this library which is not listed here? Tell us so that we can consider its inclusion in this list.

php-date-formatter is released under the BSD 3-Clause License. See the bundled LICENSE.md for details.

Note

You can now visit the Krajee Webtips Q & A forum for searching OR asking questions OR helping programmers with answers on these extensions and plugins. For asking a question click here. Select the appropriate question category (i.e. Krajee Plugins) and choose this current page plugin in the question related to field.

The comments and discussion section below are intended for generic discussions or feedback for this plugin. Developers may not be able to search or lookup here specific questions or tips on usage for this plugin.

 
visitors to Krajee Jquery Plugins since 22-May-2017