Title: Date formatting problem
Last modified: November 18, 2024

---

# Date formatting problem

 *  Resolved [gels13](https://wordpress.org/support/users/gels13/)
 * (@gels13)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/date-formatting-problem-2/)
 * when I call the frontend to show today’s date, it is not translated to “bg-BG”,
   it is shown to EN, but when it has to show another date, the translation is normal?
    -  This topic was modified 1 year, 6 months ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).
      Reason: removed nsfw
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fdate-formatting-problem-2%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [mayuripatel](https://wordpress.org/support/users/mayuripatel/)
 * (@mayuripatel)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/date-formatting-problem-2/#post-18142916)
 * 1. **Check WordPress Locale Settings**
 * WordPress uses the locale (`bg-BG`) to determine how dates (and other content)
   are displayed. First, make sure the locale is set correctly in WordPress.
    - Go to **Settings > General** in the WordPress admin panel.
    - Check the **Site Language** field and ensure that it is set to **Bulgarian**(`
      bg-BG`).
 * If the language is correct, WordPress should be translating dates properly, but
   some custom code or plugins may override this.
 * 2. **Ensure Proper Locale for PHP `date()` or `strftime()`**
 * If you’re using PHP to display the date (`date()` or `strftime()`), you need 
   to make sure that PHP is set to use the correct locale. You can set the locale
   with the `setlocale()` function in PHP.
 * Try adding the following code to your theme’s `functions.php` file to ensure 
   the correct locale is set for date-related functions:
 *     ```wp-block-code
       if (function_exists('setlocale')) {    setlocale(LC_TIME, 'bg_BG.UTF-8');}
       ```
   
 * This ensures that PHP will use the Bulgarian locale (`bg_BG.UTF-8`) when formatting
   dates.
 * 3. **WordPress Date Functions**
 * If you are using WordPress date functions like `get_the_date()`, WordPress usually
   handles the locale automatically based on your site’s language settings. However,
   in some cases, you may need to force the translation.
 * Try using the `get_date_from_gmt()` function and ensuring that the correct locale
   is set before calling it:
 *     ```wp-block-code
       $today = current_time('Y-m-d'); // or 'U' for timestampecho date_i18n(get_option('date_format'), strtotime($today));
       ```
   
 * Here, `date_i18n()` is a WordPress function that will respect the locale and 
   display the date according to your language settings.
 * 4. **JavaScript Date Locale**
 * If the date is being rendered via JavaScript (e.g., using `new Date()`), you’ll
   need to make sure that JavaScript is using the correct locale when displaying
   the date.
 * For JavaScript, the `toLocaleDateString()` method should be used to format the
   date according to the desired locale. Here’s an example:
 *     ```wp-block-code
       const today = new Date();const options = { year: 'numeric', month: 'long', day: 'numeric' };const formattedDate = today.toLocaleDateString('bg-BG', options);document.getElementById('date').textContent = formattedDate;
       ```
   
 * In this example, the `toLocaleDateString()` function ensures that the date is
   displayed in Bulgarian format.
   5. **Custom Date Formatting in Theme**For example,
   if you’re using custom code like this:
 *     ```wp-block-code
       echo date('l, F j, Y');
       ```
   
 * You can replace it with WordPress’s `date_i18n()` function to ensure proper translation:
 *     ```wp-block-code
       echo date_i18n('l, F j, Y');
       ```
   
 * 6. **Multilingual Plugins (WPML, Polylang, etc.)**
 * If you’re using a multilingual plugin like **WPML** or **Polylang**, ensure that:
    - The correct language is selected for the user viewing the page.
    - The date formatting is set correctly within the plugin settings.
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/date-formatting-problem-2/#post-18146522)
 * The problem could be caused by the current bug regarding the language files of
   themes. See: [https://core.trac.wordpress.org/ticket/62337](https://core.trac.wordpress.org/ticket/62337)
 *  Thread Starter [gels13](https://wordpress.org/support/users/gels13/)
 * (@gels13)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/date-formatting-problem-2/#post-18147785)
 * I was unable to resolve the issue that is in the “generic” Theme!
 * I created a separate site with a sub-domain, in which there is only wordpress
   and the theme, no plugins.
 * test.divine-age.com
   When I call today’s date in functions.php, via “echo date(‘
   l, F j, Y’);”, the translation is in Bulgarian, but when I use a function created
   in functions.php for a shortcode by which I call today’s date, it is in english.
   The shortcode function is:
 * function displayTodaysDate( $atts )
   {return date(get_option(‘date_format’));}
   add_shortcode( ‘datetoday’, ‘displayTodaysDate’ );where the translation is in
   English!I am not good with codes and programming for which I apologize and Please
   help!
    -  This reply was modified 1 year, 6 months ago by [gels13](https://wordpress.org/support/users/gels13/).
 *  [mayuripatel](https://wordpress.org/support/users/mayuripatel/)
 * (@mayuripatel)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/date-formatting-problem-2/#post-18147994)
 * Hello,
 * Here’s a solution to fix that:
    1. **Set the locale to Bulgarian**: You can use the `setlocale()` function to ensure
       that the date is displayed in the correct language.
    2. **Modify your shortcode function**: Update the `displayTodaysDate` function 
       to set the locale for Bulgarian before generating the date.
 * Here’s an updated version of your `functions.php` code:
 *     ```wp-block-code
       function displayTodaysDate( $atts ){    // Set the locale to Bulgarian (Bulgarian locale may vary depending on your server setup)    setlocale(LC_TIME, 'bg_BG.UTF-8', 'bg_BG', 'bg', 'bulgarian');    // Return the date in the desired format    return strftime('%A, %B %d, %Y');}add_shortcode( 'datetoday', 'displayTodaysDate' );
       ```
   
 * Key points:
    1. **`setlocale()`**: This function is used to set the current locale. We’re setting
       it to Bulgarian (`bg_BG.UTF-8`). The other variants (`bg_BG`, `bg`, `bulgarian`)
       are used as fallbacks in case one of them isn’t available on the server.
    2. **`strftime()`**: This function formats the date according to the locale set
       by `setlocale()`. It will output the date in Bulgarian. The format `'%A, %B %
       d, %Y'` is similar to the format you wanted (`l, F j, Y`), but it works with`
       strftime()`.
 * Things to check:
    - **Locale availability**: Ensure that the Bulgarian locale (`bg_BG.UTF-8`) 
      is available on your server. If it’s not, you might need to install or enable
      it on your server, or use a different format that doesn’t depend on `setlocale()`.
    - **WordPress language settings**: Make sure that your WordPress language is
      set to Bulgarian in **Settings > General**. This helps ensure that WordPress
      uses the correct translation for dates, numbers, and other locale-specific
      data.
 * With this modification, when you use the `[datetoday]` shortcode, the date should
   be displayed in Bulgarian, as long as the correct locale is supported on your
   server.
 *  Thread Starter [gels13](https://wordpress.org/support/users/gels13/)
 * (@gels13)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/date-formatting-problem-2/#post-18150448)
 * Thank you very much, you were able to help me with your guidance!
   Thank you!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Date formatting problem’ is closed to new replies.

 * In: [Everything else WordPress](https://wordpress.org/support/forum/miscellaneous/)
 * 5 replies
 * 3 participants
 * Last reply from: [gels13](https://wordpress.org/support/users/gels13/)
 * Last activity: [1 year, 6 months ago](https://wordpress.org/support/topic/date-formatting-problem-2/#post-18150448)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
