CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • A little passthrough mod for CRM_Utils_Date::customFormat
Pages: [1]

Author Topic: A little passthrough mod for CRM_Utils_Date::customFormat  (Read 885 times)

mhonman

  • I’m new here
  • *
  • Posts: 20
  • Karma: 2
A little passthrough mod for CRM_Utils_Date::customFormat
February 24, 2012, 07:58:05 am
Mark Tompsett has been doing some very interesting reports for us - reports with loads of date columns, that the users wanted to see in their CiviCRM dashboard.

So we wanted to use a very compact date format in the reports, and override the date format set in Global Settings in this case only. Mark has now produced a pretty elegant and self-contained solution, but along the way we were battling with the usually helpful formatting that the CiviCRM report engine does after the custom report's alterDisplay method.

One of the approaches we tried was to perform the date formatting in the custom report, and modify CRM_Utils_Report::customFormat to pass through any string that it could not interpret as a date. The approach I took was, after the existing date-parsing code, to reassemble a date string from the elements that resulted from parsing. If the strings do not match, obviously we didn't have an intelligible date string to start with.

In the end we didn't need to use this, as Mark found a more elegant way of dealing with the problem, but I'm posting the code here (changes in red) just in case anyone might benefit from it. The base CiviCRM version is 3.4.7.

    static function customFormat($dateString, $format = null, $dateParts = null)
    {
        // 1-based (January) month names arrays
        $abbrMonths = self::getAbbrMonthNames();
        $fullMonths = self::getFullMonthNames();

        if ( ! $format ) {
            $config = CRM_Core_Config::singleton();

            if ($dateParts) {
                if (array_intersect(array('h', 'H'), $dateParts)) {
                    $format = $config->dateformatDatetime;
                } elseif (array_intersect(array('d', 'j'), $dateParts)) {
                    $format = $config->dateformatFull;
                } elseif (array_intersect(array('m', 'M'), $dateParts)) {
                    $format = $config->dateformatPartial;
                } else {
                    $format = $config->dateformatYear;
                }
            } else {
                if ( strpos($dateString, '-') ) {
                    $month  = (int) substr($dateString,  5, 2);
                    $day    = (int) substr($dateString,  8, 2);
                } else {
                    $month  = (int) substr($dateString,  4, 2);
                    $day    = (int) substr($dateString,  6, 2);
                }

                if (strlen($dateString) > 10) {
                    $format = $config->dateformatDatetime;
                } elseif ($day > 0) {
                    $format = $config->dateformatFull;
                } elseif ($month > 0) {
                    $format = $config->dateformatPartial;
                } else {
                    $format = $config->dateformatYear;
                }
            }

        }

        if ($dateString) {
            if ( strpos($dateString, '-') ) {
                $year   = (int) substr($dateString,  0, 4);
                $month  = (int) substr($dateString,  5, 2);
                $day    = (int) substr($dateString,  8, 2);

                $hour24 = (int) substr($dateString, 11, 2);
                $minute = (int) substr($dateString, 14, 2);

                if ( strlen ($dateString) < 15 ) {
                        $reconstitutedDateString = sprintf('%04d-%02d-%02d', $year, $month, $day);
                } else {
                    $reconstitutedDateString = sprintf('%04d-%02d-%02d %02d:%02d',
                                                       $year, $month, $day, $hour24, $minute);
                }

            } else {
                $year   = (int) substr($dateString,  0, 4);
                $month  = (int) substr($dateString,  4, 2);
                $day    = (int) substr($dateString,  6, 2);

                $hour24 = (int) substr($dateString, 8, 2);
                $minute = (int) substr($dateString, 10, 2);

                if ( strlen ($dateString) < 11 ) {
                        $reconstitutedDateString = sprintf('%04d%02d%02d', $year, $month, $day);
                } else {
                    $reconstitutedDateString = sprintf('%04d%02d%02d%02d%02d',
                                                       $year, $month, $day, $hour24, $minute);
                }
            }
            if ( strncmp($reconstitutedDateString, $dateString, strlen($reconstitutedDateString)) != 0 ) {

                return $dateString;

            } else {

                if     ($day % 10 == 1 and $day != 11) $suffix = 'st';
                elseif ($day % 10 == 2 and $day != 12) $suffix = 'nd';
                elseif ($day % 10 == 3 and $day != 13) $suffix = 'rd';
                else $suffix = 'th';

                if ($hour24 < 12) {
                    if ($hour24 == 00) $hour12 = 12;
                    else $hour12 = $hour24;
                    $type = 'AM';
                } else {
                    if ($hour24 == 12) $hour12 = 12;
                    else $hour12 = $hour24 - 12;
                    $type = 'PM';
                }

                $date = array(
                              '%b' => CRM_Utils_Array::value( $month, $abbrMonths ),
                              '%B' => CRM_Utils_Array::value( $month, $fullMonths ),
                              '%d' => $day > 9 ? $day : '0' . $day,
                              '%e' => $day > 9 ? $day : ' ' . $day,
                              '%E' => $day,
                              '%f' => $suffix,
                              '%H' => $hour24 > 9 ? $hour24 : '0' . $hour24,
                              '%h' => $hour12 > 9 ? $hour12 : '0' . $hour12,
                              '%I' => $hour12 > 9 ? $hour12 : '0' . $hour12,
                              '%k' => $hour24 > 9 ? $hour24 : ' ' . $hour24,
                              '%l' => $hour12 > 9 ? $hour12 : ' ' . $hour12,
                              '%m' => $month  > 9 ? $month  : '0' . $month,
                              '%M' => $minute > 9 ? $minute : '0' . $minute,
                              '%i' => $minute > 9 ? $minute : '0' . $minute,
                              '%p' => strtolower($type),
                              '%P' => $type,
                              '%A' => $type,
                              '%Y' => $year
                              );

                return strtr($format, $date);
            }
        } else {
            return '';
        }

    }


 

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • A little passthrough mod for CRM_Utils_Date::customFormat

This forum was archived on 2017-11-26.