0

Basically, I just want the format to be mdY if an American / US Resident is using the site, and dmY for everyone else. Right now, the only way I see I can do this is changing it via radio buttons. Is there a hook or some piece of code I can put in instead? Should be pretty simple but I Googled everywhere and could not find the answer.

4 Answers 4

0

WordPress is not multilingual out of the box. It needs a third party plugin to be. The date will be displayed as defined in the settings according the website language also defined in the WordPress settings?

Regards

0

If you're wanting the rewrite to happen if the person is from the US, you'll likely need a GeoIP plugin to detect where the user is from, and then edit the date format display accordingly. Instead of a GeoIP (Geographic IP) detection, you could have a popup on your site asking where the visitor is from. This will get annoying, but it's another way of gathering info.

If you're only dealing with people who are logged in, you could create a custom field in the user meta during signup asking for their country (in the sample below, I used "country" as the meta field.)

At that point, you can use the field to change the format for the display using the "date_format" command in a WordPress function;

function change_date_format($date_format) {
    $current_user = wp_get_current_user();
    $user_country = get_user_meta($current_user->ID, 'country', true);
    if ($user_country === 'US') {
        $date_format = 'm/d/Y';
    }
    return $date_format;
}
add_filter('date_format', 'change_date_format');

In the above code, if the field explicitly answers "US" then change the format to be m/d/y, otherwise, proceed with the default selected in the WordPress settings.

0

I agree with previous anwser but if you still want to achieve this not using a third party plugin I would recommend to do like so :

  1. Set your custom date format as a WP option
set_option('us_date_format', 'F j Y'); //Should be adapted to your needs
  1. Detect the current locale from user browser using javascript (better to enqueue it into a separate .js file) :
jQuery(document).ready(function ($) {
 
  if (navigator.languages != undefined || navigator.languages[0] !== 'en-US') {
    return;
  }

  // Ajax call
  $.ajax({
    url: ajax_object.ajax, //you need to define your ajax_object
    type: "POST",
    data: {
      action: "set_session_lang",
    },
    success: function () {
      console.log("Custom locale set !");
    },
  });
});

(beware navigator.language isn't supported by old browsers)

  1. PHP function, called with Ajax, that change your current locale dynamically (in your function.php) :
<?php
add_action( 'wp_ajax_nopriv_set_session_lang', 'set_session_lang' );
function set_session_lang() {
    global $wp_session;

    $wp_session['lang'] = 'en-US'; // A string

    exit(); //Always exit() from an ajax call.
    }
?>
  1. Display post time in the right format (in your template.php)
add_filter( 'the_time', 'us_date_format', 10, 1 );
function us_date_format( $date ) {

    global $wp_session; 

    if(  $wp_session['lang']  !== 'en-US' ){
    return $date;
    }

    global $post;

    $date = get_post_time( get_option( 'us_date_format' ), false, $post, true ); 

    return $date;
    } 
}

This solution might be adapt to your needs, but I hope it will help you to achieve your goal.

0

Basically this can not been done server side and you have to do it in JS. There is JS API to display date and time information based on user's locale like https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

and the question becomes now how do you transfer the relevant date information from the server to the JS, probably as a JS variable.

this assumes users set their browsers locale to the actual locale they are in which is not true in all kind of situations, but still should be probably good enough for 95+% of the cases.

will correct myself, the locale information and language are being sent by the browser so in theory it can be done server side, but the browser have information regarding OS setting which are not being passed therfor the better solution is to use JS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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