Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: How to Format Dates as 1-Jan-2010

  1. #1
    datasponge is offline Sugar Community Member
    Join Date
    Mar 2008
    Location
    San Jose, CA, USA
    Posts
    553

    Default How to Format Dates as 1-Jan-2010

    I want to display dates as d-mmm-YYYY (e.g. 1-Jan-2010) or d-mmm-YY (e.g. 1-Jan-10).

    Neither are listed as an option in the Locale dropdown for date format. All formats are number only displays. I'm using SugarCRM 5.2.0f.

    This is easy in MySQL using DATE_FORMAT(<date>, '%d-%b-%Y')

    It is easy in php using date_format(<date>, 'd-M-Y')

    How can I add this date format so that it can be selected in the Locale dropdown.

    Thanks

    Phil

  2. #2
    datasponge is offline Sugar Community Member
    Join Date
    Mar 2008
    Location
    San Jose, CA, USA
    Posts
    553

    Default Re: How to Format Dates as 1-Jan-2010

    I'm still working on this so any help is appreciated.

    I found the file <sugarcrm>/include/TimeDate.php. It contains the definition of the TimeDate class and defines two arrays that look relevant:

    1. $supported_strings appears to list the supported locale formats
    2. $time_token_map per comments lists the mapping from $supported_strings to php date() format strings


    But my crude attempts at changing these arrays didn't change the result.

    Has anyone tried this?

    Phil

  3. #3
    datasponge is offline Sugar Community Member
    Join Date
    Mar 2008
    Location
    San Jose, CA, USA
    Posts
    553

    Default Re: How to Format Dates as 1-Jan-2010

    Still working on this, any ideas?

    The only idea I can think of is to create after_retrieve and before_save logic hooks for every module that traverse the bean and reformat and unformat every date field, but that seems quite ridiculous for such a simple problem.

    There's got to be a simple way to do this.

    Phil

  4. #4
    datasponge is offline Sugar Community Member
    Join Date
    Mar 2008
    Location
    San Jose, CA, USA
    Posts
    553

    Default Re: How to Format Dates as 1-Jan-2010

    OK, this doesn't turn out to be easily done:

    1. The 5.5 Developer's Manual says
    It is not possible to hook logic to beans being displayed in a SubPanel.
    2. Dates in Edit View are limited to 10 characters, so 10-Feb-2010 gets truncated to 10-Feb-201
    3. My logic hooks are not firing in List View for some reason (probably debug-able)
    4. Changes to <sugarcrm>/include/TimeDate.php involve many separate functions for conversion, popup calendar formatting, db formatting, display formatting, etc so would be fairly extensive.
    5. Changes to <sugarcrm>/include/TimeDate.php would be considered a core hack and because of 4 would be likely to break other Sugar behaviors and possibly corrupt dates.


    So my boss won't be happy, but this doesn't look like a managable change.

    Any idea if Sugar plans to support additional Locale date formats in future releases?

    Thanks

    Phil

  5. #5
    ptemplin7 is offline Sugar Community Member
    Join Date
    Jun 2007
    Location
    Puget Sound Washington
    Posts
    380

    Cool Re: How to Format Dates as 1-Jan-2010

    Make sure this ends up in the feature request bin.

    It's kind of like the phone number formatting. I'm using logic hooks to make it look nice and make sure to strip the 16 different ways users tend to insert phone numbers when creating a record!

    The worst part of it is that there appears to be a standard javascript for handling the formatting but is not used!

    My guess is this is one of those options that should have been looked at a long time ago but is now so intertwined in the code it is difficult to fix.

    Thanks for the efforts. Unfortunately I no help to give!

    Paris

  6. #6
    agcopley is offline Sugar Community Member
    Join Date
    Nov 2007
    Location
    Santiago, Chile
    Posts
    204

    Default Re: How to Format Dates as 1-Jan-2010

    Quote Originally Posted by datasponge View Post
    Still working on this, any ideas?

    The only idea I can think of is to create after_retrieve and before_save logic hooks for every module that traverse the bean and reformat and unformat every date field, but that seems quite ridiculous for such a simple problem.

    There's got to be a simple way to do this.

    Phil
    Hi Phil,
    The available date formats are specified in the config.php file in root. These date formats are then converted into real php dateformats using the arrays specified. Augmenting both the config file to include your new format and the arrays to translate it in TimeDate.php (build_format()) should work (but I havent tried it :-))

    Slds
    Andrew

  7. #7
    datasponge is offline Sugar Community Member
    Join Date
    Mar 2008
    Location
    San Jose, CA, USA
    Posts
    553

    Default Re: How to Format Dates as 1-Jan-2010

    Quote Originally Posted by agcopley View Post
    Hi Phil,
    The available date formats are specified in the config.php file in root. These date formats are then converted into real php dateformats using the arrays specified. Augmenting both the config file to include your new format and the arrays to translate it in TimeDate.php (build_format()) should work (but I havent tried it :-))

    Slds
    Andrew
    Thanks Andrew,

    I spent some time (too much) looking at TimeDate.php and concluded it would be too dangerous to mess with. There are just too many cross-related functions that must be updated to do it. You have to convert to and from database format, to and from Locale format, to and from js calendar format, and there is a string length limitation of 10 characters somewhere so that I can' t even edit in a logic hook and display 10-Feb-2010 -- it gets truncated to 10-Feb-201.

    So if I screw it up, I can wind up with incorrect dates in the database, possibly transparently in combinations easy to overlook.

    It should be easier once Sugar supports php 5.3 since it has much richer date handling.

    Phil

  8. #8
    datasponge is offline Sugar Community Member
    Join Date
    Mar 2008
    Location
    San Jose, CA, USA
    Posts
    553

    Default Re: How to Format Dates as 1-Jan-2010

    Hi All,

    I got date formatting working in all except edit views by adding two logic hooks:
    1. after_retrieve that identifies date fields and reformats them
    2. process_record that simply does a retrieve

    The process_record works for list view and subpanels (despite what the Developer's Guide says). The after_retrieve gets called from the process_record retrieve, so I don't even need to check fields for date formatting.

    There are still two major issues:
    1. Edit View does not allow more than 10 characters in a date field and uses more than 10 to determine if it is a datetime field in some places. Also if I format the date in Edit View, I would need to unformat it before it is saved or else the function to format it back to db format would not recognize this format and would fail.
    2. This absolutely kills performance of the Accounts Details page since it must iterate through all fields for all subpanel data. Server Response time is reported as almost 4 seconds with these hooks installed.

    Phil

  9. #9
    datasponge is offline Sugar Community Member
    Join Date
    Mar 2008
    Location
    San Jose, CA, USA
    Posts
    553

    Default Re: How to Format Dates as 1-Jan-2010

    I was using process_record incorrectly. It is fired after it has run the query and returned data. So by calling retrieve, I was doing double-work and so killing performance.

    Once I sorted out how to correctly use process_record, I can handle everything except edit view issues. I simply process the dates from both hooks and make sure that I don't handle any bean twice.

    If anyone wants to code to reformat dates to a format like 10-Feb-2010 in DetailView, ListView and SubPanels, let me know and I'll post it.

    Phil

  10. #10
    DJuser is offline Sugar Community Member
    Join Date
    May 2007
    Posts
    16

    Default Re: How to Format Dates as 1-Jan-2010

    Quote Originally Posted by datasponge View Post
    If anyone wants to code to reformat dates to a format like 10-Feb-2010 in DetailView, ListView and SubPanels, let me know and I'll post it.

    Phil
    Please do tell us the details of how you did it.

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to set up due dates based on business dates
    By sugar7478 in forum Developer Help
    Replies: 0
    Last Post: 2009-02-27, 02:27 PM
  2. Replies: 0
    Last Post: 2009-01-22, 05:58 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •