korosaki zaki
korosaki zaki

Reputation: 31

Php mysql date in table

I have a date field in my table material

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

Upvotes: 1

Views: 75

Answers (2)

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146450

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.

Upvotes: 1

Qirel
Qirel

Reputation: 26450

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:

Upvotes: 1

Related Questions