0

in my original code the date was wrapped with date_formatted. my website is bilingual so i need to localize date format and name of months etc. i've changed it with wp_date but it does not work.

the original code. the output of date_format is only english so it is not good for me.

function getDateSF ( $start, $end ) {

    $_start = [ date_format( $start, __('F d', 'my-theme') ), date_format( $start, __('g:i a', 'my-theme') ) ];
    $_end = [ date_format( $end, __('F d', 'my-theme') ), date_format( $end, __('g:i a', 'my-theme') ) ];

    $date = '<div class="transl">'.$_start[0] .' | '. $_start[1] .'  '. __('to', 'my-theme') .' '. $_end[1] .'</div>';

    if ( date_format( $start, 'Y/m/d' ) != date_format( $end, 'Y/m/d' ) )
        $date = '<div class="transl">'. $_start[0] .' | '. $_start[1] .' </div><div class="transl"> '. $_end[0] .' | '. $_end[1] .'</div>';

    return $date;
}

wp_date is good for localization but it doesn't work:

function getDateSF ( $start, $end ) {

    $_start = [ wp_date( __('F d', 'my-theme'), $start ), wp_date( __('g:i a', 'my-theme'),  $start ) ];
    $_end = [ wp_date(  __('F d', 'my-theme'),$end ), wp_date(  __('g:i a', 'my-theme'),$end ) ];

    $date = '<div class="transl">'.$_start[0] .' | '. $_start[1] .'  '. __('to', 'my-theme') .' '. $_end[1] .'</div>';

    if ( date_format( $start, 'Y/m/d' ) != date_format( $end, 'Y/m/d' ) )
        $date = '<div class="transl">'. $_start[0] .' | '. $_start[1] .' </div><div class="transl"> '. $_end[0] .' | '. $_end[1] .'</div>';

    return $date;
}

edit

wp_date only accepts unix timestamp so the solution is wrapping variables like $start and $end with date_timestamp_get().

wp_date( __('F d', 'my-theme'),  date_timestamp_get($start)  );

1 Answer 1

2

I was able to make my local WP installation display the date in French:

switch_to_locale( 'fr_CA' );
echo wp_date( 'F d' );

Output: octobre 11

(Note that I didn't have to localize my date format string (ie, no __() inside the wp_date() call.)

However, when I tried Dutch:

switch_to_locale( 'nl_NL' );
echo wp_date( 'F d' );

Output: October 11

This is because the French (fr_*) language packs have been installed, but not the Dutch ones (nl_*).

Are you sure the required language packs are installed on your server? If not, check with your hosting provider.

References

4
  • without "__()" it does not work either. Oct 11, 2022 at 19:56
  • Are you sure the language pack(s) are installed? If they're not, any attempt to switch the locale will fail over to the default (presumably English).
    – Pat J
    Oct 11, 2022 at 20:25
  • yep it is installed. wp_date( 'F d', $start) and others don't work. though i don't get how to update the current code with your solution. Oct 11, 2022 at 20:42
  • If you insert var_dump( get_locale() ); before your code, what locale are you in? If it's en_{something} then you need to somehow switch to the desired locale before you use wp_date().
    – Pat J
    Oct 11, 2022 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.