ymd, dmy, mdy in php

367 views
Skip to first unread message

Nick

unread,
Mar 28, 2017, 3:46:59 PM3/28/17
to Easy!Appointments - Support Group
Hello,

Similar to the previous post, there exists a function in .js that allows us to get the system settings on the preferred date format. That being said, in this case, it is all via .js and I dont find a similar function in php files. I need to be able to use that function in the php file.

Tips and pointers would be appreciated.

Thanks,
Nick

Craig Tucker

unread,
Mar 29, 2017, 9:33:34 AM3/29/17
to Easy!Appointments - Support Group
So you are trying to convert the date format?  You can use date and strotime.  An example from my CLI reminders file is:

                $aptdatetime=date('D g:i a',strtotime($result["start_datetime"]));
                $startdatetime
=date('l, F j, Y, g:i a',strtotime($appointment->start_datetime));

You will need to look up the php date formatting options to decide on the letters for your format.

Nick

unread,
Mar 29, 2017, 11:05:22 AM3/29/17
to Easy!Appointments - Support Group
Hi,

I am wondering if there is a helper function that Alex built into the application. As an example, in backend_calendar_appointments_modal.js, there is this snippet of code:

        var startDatetime = new Date().addMinutes(GlobalVariables.bookAdvanceTimeout);
        var endDatetime  = new Date().addMinutes(GlobalVariables.bookAdvanceTimeout).addMinutes(serviceDuration);
        var dateFormat;
        switch(GlobalVariables.dateFormat) {
            case 'DMY':
                dateFormat = 'dd/mm/yy';
                break;
            case 'MDY':
                dateFormat = 'mm/dd/yy';
                break;
            case 'YMD':
                dateFormat = 'yy/mm/dd';
                break;
            default:
                throw new Error('Invalid GlobalVariables.dateFormat value.');
        }

So there is a nice GlobalVariable that is called dateFormat, and essentially based on the settings in the General Tab of backend, if you set it to DMY, MDY, or YMD, then this above snippet is able to know what was set so that the proper case statement and hence date format can be used.

Now, I am in need of the same thing from a php file. WaitingList.php which is part of Craig's cli.

In general_functions.js the following is found:
    /**
     * Format a given date according to the date format setting.
     *
     * @param {Date} date The date to be formatted.
     * @param {String} dateFormatSetting The setting provided by PHP must be one of
     * the "DMY", "MDY" or "YMD".
     * @param {Boolean} addHours (optional) Whether to add hours to the result.
     * @return {String} Returns the formatted date string.
     */
    exports.formatDate = function(date, dateFormatSetting, addHours) {
        var format, result;
        var hours = addHours ? ' HH:mm' : '';
        switch(dateFormatSetting) {
            case 'DMY':
                result = Date.parse(date).toString('dd/MM/yyyy' + hours);
                break;
            case 'MDY':
                result = Date.parse(date).toString('MM/dd/yyyy' + hours);
                break;
            case 'YMD':
                result = Date.parse(date).toString('yyyy/MM/dd' + hours);
                break;
            default:
                throw new Error('Invalid date format setting provided!', dateFormatSetting);
        }
        return result;
    };

I want to be able to apply the proper date_format and time_format based on the values found in the backend general settings to your WaitingList.php file. This way, if people set it to 24hr format and set it to YMD, they will end up with 2017/03/28 19:00. Or any other combination.

Nick



Reply all
Reply to author
Forward
0 new messages