1

I have a date field in my table material

  • id: int;
  • dat_operation : date;
  • name : varchar(255);

I would like to know how to translate date format in French I tried with:

<?php  echo date("F j Y",strtotime($var['date_operation']));?>

But i have this result

June 14 2016
6
  • Could you define what the desired output is? (Not everybody knows all the different date-formats from around the world...) Commented Jun 14, 2016 at 14:13
  • Or do you want "June" beeing translated to french? Commented Jun 14, 2016 at 14:14
  • @Jeff yes, i want to translate June in french Commented Jun 14, 2016 at 14:15
  • those answers might help: stackoverflow.com/questions/1328036/… Commented Jun 14, 2016 at 14:16
  • You can use setlocale() to translate the language of the date: stackoverflow.com/questions/2765247/… Commented Jun 14, 2016 at 14:16

2 Answers 2

1

First, you'll have to set the "locale information", to specify which language you want to use. Keep in mind, that even though you set that language, it needs to be installed on the server you're running on. It most likely is, but you'll notice if the setlocale has no effect (default is English).

The second thing you'll need to know, is that date() isn't affected by this, you'll have to use strftime() instead, which has a slightly different formatting, which you'll find on the documentation.

An example of using French dates with these two functions:

setlocale(LC_ALL, 'fr_FR'); 
echo strftime("%B %e %Y", strtotime($var['date_operation']));

Reference and documentation:

Sign up to request clarification or add additional context in comments.

Comments

1

The modern and rock-solid approach is the intl (from "Internationalization") extension, which offers e.g. the IntlDateFormatter class:

$date = new DateTime('2016-06-14');
$fmt = new IntlDateFormatter('fr_FR', IntlDateFormatter::LONG, IntlDateFormatter::NONE, 'Europe/Paris', IntlDateFormatter::GREGORIAN);
var_dump($fmt->format($date));
string(12) "14 juin 2016"

If you think it's overkill for your project, you can use the legacy strftime() function but you need to change current locale:

$date = strtotime('2016-06-14');
var_dump(setlocale(LC_TIME, 'fr_FR', 'fr')); // Need to try values until you get true
var_dump(strftime('%B %e %Y', $date));

You need to have French locale data installed. In my experience, this works better on Unix-like systems than on Windows.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.