2

I am working on a php/javascript code below which returns month names in French but it doesn't match the french words which we are using.

<script>
document.getElementById('title_fr').value = "<?php setlocale(LC_TIME, "frc"); echo strftime("%d %b %Y", strtotime( $this_date )); ?>"; 
</script>

The above script returns the month names in french which is not matching our set of month names in french.

mars
avr.
mai
juin
juil.
août
sept.
oct. 
nov.
déc.

Following are the set of month names in French which we follow:

janvier
février
mars
avril
mai
juin
juillet
août
septembre
octobre
novembre
décembre

Problem Statement:

I am wondering what changes I need to make in the script above so that it returs month name in french which I am following.

I believe I need to create an array as shown below with the month names in french.

const monthNamesFr = ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"];
john
  • 11,311
  • 40
  • 131
  • 251
  • So in (your) France, a year has only ten months then, that’s interesting :-) – 04FS Mar 14 '19 at 14:39
  • According to the manual on [`strftime`](http://php.net/manual/en/function.strftime.php) you can use `%B` instead of `%b` to get the full month name (based on locale). Would that solve the issue? – JNevill Mar 14 '19 at 14:40
  • No, it has 12 months. I forgot to put Jan/Feb. – john Mar 14 '19 at 14:40
  • Use `date` to get only the month part of your date, and then use that as the index to access your month name array. Concatenate that with the rest of the date parts you need to output before or after. – 04FS Mar 14 '19 at 14:40
  • @JNevill It worked, thx for the help. Do you think I need to need to make any changes here `setlocale(LC_TIME, "frc"); echo strftime("%d %b %Y", strtotime( $this_date ));` ? – john Mar 14 '19 at 14:45
  • @JNevill The other issue which I am facing is when its décembre and février, its returning d�cembre and f�vrier – john Mar 14 '19 at 14:48
  • I think `setlocale(LC_TIME, "frc"); echo strftime("%d %B %Y", strtotime( $this_date ));` is the right way to go. As for your ugly characters, it sounds like a character encoding issues (UTF8 Characters coming through but being converted to ASCII). Not being a PHP expert, I'm not sure you handle that. If there is a setting in your php.ini that needs changing or if you handle encoding conversion with something like `mb_convert_encoding()`... I would pop open another question here on SO with that so it can get the right experts eyes on it. – JNevill Mar 14 '19 at 15:01
  • From the Dave Burton answer https://stackoverflow.com/questions/241015/question-mark-characters-displaying-within-text-why-is-this I used `` and it started working. – john Mar 14 '19 at 15:03
  • I am not sure if its effect other parts of the code. – john Mar 14 '19 at 15:04

2 Answers2

1

Since this seems to be doing the trick (from the comments section). Instead of hardcoding your dates in an array, just request the full month from the strftime() function:

 setlocale(LC_TIME, "frc"); echo strftime("%d %B %Y", strtotime( $this_date ));
JNevill
  • 46,980
  • 4
  • 38
  • 63
  • It worked. The other issue which I am facing is when its décembre and février, its returning d�cembre and f�vrier – john Mar 14 '19 at 15:05
1

I'd suggest using a valid locale such as fr_FR for french language in France. Additionally, use %B for the full month name per the strftime() documentation.

$this_date = '2018-04-03 12:12:12';
setlocale(LC_TIME, "fr_FR");
echo strftime('%d %B %Y', strtotime($this_date)); // 03 avril 2019

This works regardless of the system's configured locale.

Erik Giberti
  • 1,235
  • 9
  • 11
  • It worked. The other issue which I am facing is when its décembre and février, its returning d�cembre and f�vrier – john Mar 14 '19 at 15:14
  • That’s likely a character encoding issue. If you are mixing UTF-8 with some other encoding like iso-8859-1 – Erik Giberti Mar 17 '19 at 11:54