I needed a date/time format to be in Chinese. Example: 2013年 4月 30日 - 13:00. Where 年 = Year, 月 = Month, 日 = date.
After creating a date format with chinese input Y年 n月 j日 - H:i, the time display got messed up.

To reproduce this bug:

  1. Create a Date Format Y年 n月 j日 - H:i
  2. Add a Date Type Chinese and select the Date Format from above
  3. Create a Content Type with start and end date. Granularity to the minute.
  4. Click on Manage Display tab. Change the date display to the Date Format type you have just created.
  5. Create a Node from the new Content Type and enter a start date and end date with a difference of 1 hour. Example: 30 April 2013 - 13:00 AND 30 April 2013 - 14:00

The output: 2013年 4月 30日 - 13:00 年月日, which is wrong.

The correct output should be: 2013年 4月 30日 - 13:00 to 14:00

After digging into the module, I found out that the function date_formatter_process is not stripping non-english characters from the formatted time and timezone.

Thankfully, the Date API allowed me to quickly hook into and clean up the formatter with the non-english string(or whatever you call it).

function mymodule_date_formatter_dates_alter(&$dates) {
  $dates['value']['formatted_time'] = preg_replace('/[^\00-\255]+/u', '', $dates['value']['formatted_time']); // Strip non-english characters
  $dates['value2']['formatted_time'] = preg_replace('/[\s-]+/u', '', $dates['value2']['formatted_time']);// Strip hyphens and spaces
  $dates['value2']['formatted_time'] = preg_replace('/[^\00-\255]+/u', '', $dates['value2']['formatted_time']); // Strip non-english characters

  $dates['value']['formatted_timezone'] = preg_replace('/[^\00-\255]+/u', '', $dates['value2']['formatted_timezone']); // Strip non-english characters
  $dates['value2']['formatted_timezone'] = preg_replace('/[^\00-\255]+/u', '', $dates['value2']['formatted_timezone']); // Strip non-english characters
}

Comments

monish_deb’s picture

There is a similar issue When placing an '@' sign in a custom Date Format, it moves it to the end of the string. for which special character/non-latin character were not stripped from the formatted time and timezone due to which it is appended at the end of the date string. The patch I have provided was just to highlight the possible spot where to fix the changes (as overridden by your Date API ), but I think there should be a generalize solution to fix this issue like suggested at #3.

nocean’s picture

I have the exact same problem as the OP (right down to the Chinese characters). Despite being a bit hackish, the custom module hook worked like a charm (thanks logii). Looking forward to a fix making its way into the core module.

steinmb’s picture

Version: 7.x-2.6 » 7.x-2.x-dev