⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Format date in column using callbackColumn



Marty

Marty
  • profile picture
  • Member

Posted 20 September 2017 - 12:37 PM

I'd like to view the date columns as: 2017-09-20 14:00:00. 

 

$crud->callbackColumn(
'event_datetime', function ($value, $row) {
return date('Y-m-d H:i:s', strtotime($value));
}
);

 

But date is still shown as: 20-09-2017 2:00:00 PM

 

GorceryCRUD Enterprise version 2.3.3

 


Marty

Marty
  • profile picture
  • Member

Posted 22 September 2017 - 07:11 AM

I'm also unable to use the Quick Search when DateTime is used in the column.

Tried to fill in the default data format and variations, all return zero rows..

 

Please confirm if this is a bug, as for not being able to change date (time) format with callbackColumn.

 

Thank you in advance.


web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 25 September 2017 - 11:22 AM

Hello Marty,
 
Grocery CRUD Enterprise is using dates with a different way from Community Edition. We are getting lot of requests for the date changes as this change wasn't documented. The main difference is that the date is transformed into the local time format and language. We will give you an example to be more clear.
 
If the person is using your project from USA (and the laptop is configured with USA local time), the date will translated from: 2017-08-20 to 08/20/2017 and the language will be in English. If the person is using the web application from Greece (or change the settings to Greek settings) the date will be transformed as: 20/08/2017 and the translation will be in Greek. The main reason that we did that is in order to avoid common date issues like format, lang strings... e.t.c. that we had with community edition.
 
The above feature may not seem that obvious on browsers but on mobile devices it is really helpful as the user can input the date with the calendar of their mobile and see the date as they configured it in their mobile phone.
 
Now although this new feature took extra effort from us to create it (we could just copy the date functionality from community really) we are getting lot of requests from developers that they want the old way of adding dates. Same for all the browsers and same for all the devices. As we still believe that the local time format is the best, we have it in our backlog to add <strong>the option</strong> to the developer to use the older functionality the dates (date format/language will be unique to everyone)
 
If you are interested to this functionality please let me know by sending me an email at info@grocerycrud.com so I can inform you once this functionality is available.
 
Until then for your request a work-around for your needs (to have the sql date everywhere):
 
1. Open with your editor the file: /grocery-crud/js/build/grocery-crud-v2.3.3.fb12568.js and at around line 9260 you will see a function getFormatter that will look like this:
    getFormatter: function getFormatter() {
        if (this.dateFormatter === null) {

            if (typeof Intl !== 'undefined') {
                this.dateFormatter = new Intl.DateTimeFormat(DateAndTime.getLocale());
            } else {
                this.dateFormatter = {
                    format: function format(myDate) {
                        return myDate.toLocaleDateString();
                    }
                };
            }
        }

        return this.dateFormatter;
    },

2. Change the above function to the following:

 

getFormatter: function getFormatter() {
    if (this.dateFormatter === null) {
        this.dateFormatter = {
            format: function format(myDate) {
                // sql date
                return myDate.toISOString().slice(0, 10);
            }
        };
    }
    return this.dateFormatter;
},

With these two steps you will see that the date format will be the sql date as requested into your datagrid.

 

Regards

Johnny


Marty

Marty
  • profile picture
  • Member

Posted 28 September 2017 - 09:33 AM

Hi Johnny,

 

Thank you very much for the comprehensive response, all clear now. Though in my opinion the developer should have the possibility to choose how to implement a date format.

 

In addition to the Date column format, my current format is for example shown as "23-9-2017 10:15:00 AM".

The Quick search will only return rows for digits, i.e. 23 or 9 or 2017.

Typing 23-9 or 9-2017 or AM returns no rows.


Marty

Marty
  • profile picture
  • Member

Posted 06 October 2017 - 12:35 PM

Regarding the suggested code change, with GCE v 2.3.4,  
it will not work as expected in Safari.
 
getFormatter: function getFormatter() {
    if (this.dateFormatter === null) {
        this.dateFormatter = {
            format: function format(myDate) {
                // sql date
                return myDate.toISOString().slice(0, 10);
            }
        };
    }
    return this.dateFormatter;
},
 
Safari 11.0 (12604.1.38.1.7) will show empty fields or I should say the field is not rendered because Quick search works.
 
On Chrome 60.0.3112.113 date time column shows, 2017-10-01 12:00:00 AM
so time is not in 24h format.  
 
Firefox 56.0 will show expected result, 2017-10-01 00:00:00
And Quick search behaves as expected.

 


cletourneau

cletourneau
  • profile picture
  • Member

Posted 06 October 2017 - 15:24 PM

Even though I don't agree with the decision (developer should be allowed to force the date format, in multiple case that is mandatory), the behaviour explained by John is acceptable for me.

Problem is, in my case, the library doesn't behave like John describes.

 

As you can see in the following screenshot, the dates from the edit dialogs are different than the one displayed in the columns.

 


cletourneau

cletourneau
  • profile picture
  • Member

Posted 06 October 2017 - 16:06 PM

Also, as you can see in the following screenshots, the date typed by the user is 1 day different than the one displayed. In the database, the date is the one typed by the user.

 

 


ncasolajimenez

ncasolajimenez
  • profile picture
  • Member

Posted 17 October 2017 - 13:46 PM

 

Hello Marty,
 
Grocery CRUD Enterprise is using dates with a different way from Community Edition. We are getting lot of requests for the date changes as this change wasn't documented. The main difference is that the date is transformed into the local time format and language. We will give you an example to be more clear.
 
If the person is using your project from USA (and the laptop is configured with USA local time), the date will translated from: 2017-08-20 to 08/20/2017 and the language will be in English. If the person is using the web application from Greece (or change the settings to Greek settings) the date will be transformed as: 20/08/2017 and the translation will be in Greek. The main reason that we did that is in order to avoid common date issues like format, lang strings... e.t.c. that we had with community edition.
 
The above feature may not seem that obvious on browsers but on mobile devices it is really helpful as the user can input the date with the calendar of their mobile and see the date as they configured it in their mobile phone.
 
Now although this new feature took extra effort from us to create it (we could just copy the date functionality from community really) we are getting lot of requests from developers that they want the old way of adding dates. Same for all the browsers and same for all the devices. As we still believe that the local time format is the best, we have it in our backlog to add <strong>the option</strong> to the developer to use the older functionality the dates (date format/language will be unique to everyone)
 
If you are interested to this functionality please let me know by sending me an email at info@grocerycrud.com so I can inform you once this functionality is available.
 
Until then for your request a work-around for your needs (to have the sql date everywhere):
 
1. Open with your editor the file: /grocery-crud/js/build/grocery-crud-v2.3.3.fb12568.js and at around line 9260 you will see a function getFormatter that will look like this:
    getFormatter: function getFormatter() {
        if (this.dateFormatter === null) {

            if (typeof Intl !== 'undefined') {
                this.dateFormatter = new Intl.DateTimeFormat(DateAndTime.getLocale());
            } else {
                this.dateFormatter = {
                    format: function format(myDate) {
                        return myDate.toLocaleDateString();
                    }
                };
            }
        }

        return this.dateFormatter;
    },

2. Change the above function to the following:

getFormatter: function getFormatter() {
    if (this.dateFormatter === null) {
        this.dateFormatter = {
            format: function format(myDate) {
                // sql date
                return myDate.toISOString().slice(0, 10);
            }
        };
    }
    return this.dateFormatter;
},

With these two steps you will see that the date format will be the sql date as requested into your datagrid.

 

Regards

Johnny

 

 

Hi, and there is a way to force the date format? For example i'm using PHP desktop, so probably GC think my language is English, and i want to show it in spanish. Thanks!


web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 17 October 2017 - 21:15 PM

Hello guys,

 

As I am getting lot of questions for the date and datetime I did create a topic that I hope I am answering all of your questions. The URL is here: /topic/4067-grocery-crud-enterprise-date-and-datetime-fields/


mjuliand

mjuliand
  • profile picture
  • Member

Posted 18 November 2018 - 16:49 PM

Hard way to do it:

 

Edit Grocery_CRUD.php in libraries folder

 

about in line 295 change this:

list($hours,$minutes) = explode(":",substr($value,11));
$value = date($this->php_date_format." - H:i", mktime ((int)$hours , (int)$minutes , 0, (int)$month , (int)$day ,(int)$year));

to this:

list($hours,$minutes,$seconds) = explode(":",substr($value,11));
$value = date($this->php_date_format." - H:i:s", mktime ((int)$hours , (int)$minutes , (int)$seconds, (int)$month , (int)$day ,(int)$year));

rkrajnik

rkrajnik
  • profile picture
  • Member

Posted 26 July 2020 - 19:55 PM

Johnny knows his team needs to fully fix their date/time storage and display date-related code.  It's way too confusing (most of my apps are date-sensitive, I just assumed that GCE would handle date formats normally.)  Johnny needs to listen a bit more to his customers and not an "industry" that he seems to think he needs to follow.  I expect this to get better , Johnny is a good man!