3

I am working on a php code as shown below:

<?php <time datetime="<?php  echo esc_attr(date_format($ts, 'H:i d-m-Y'))  ?>"
data-timezone="<?php  echo esc_attr($tz_param)  ?>"><?php echo esc_html(date_format($ts, 'F j  H:i')) ?></time> ?> // Line A 

Line A returns the following date on the webpage:

July 10 21:30

print_r($ts) prints:

DateTime Object
(
    [date] => 2019-07-10 21:30:00.000000
    [timezone_type] => 3
    [timezone] => America/New_York
)
July 10  21:30

Problem Statement:

I am wondering what changes I should make in the php code above at Line A above so that when the page is in french, it should return the date in french.

This is what I have tried but it is still returning the date in Engllish.

<?php if(ICL_LANGUAGE_CODE=='fr'){
setlocale(LC_TIME, 'fr_FR');
?>
<time datetime="<?php echo esc_attr(date_format($ts, 'H:i d-m-Y')) ?>"
   data-timezone="<?php echo esc_attr($tz_param) ?>"><?php echo strftime(esc_html(date_format($ts, 'F j  H:i'))) ?></time> // Line B
<?php } ?>

Line B above still returns english.

flash
  • 1,455
  • 11
  • 61
  • 132
  • Possible duplicate of [change month name to french](https://stackoverflow.com/questions/7309960/change-month-name-to-french) – Gufran Hasan Jul 11 '19 at 11:11
  • @GufranHasan not really. This question has different answers that are applicable and better suited to this particular instance due to the WordPress framework. – Frits Jul 11 '19 at 12:35
  • But also using PHP functions `strftime` and `setlocale`. – Gufran Hasan Jul 11 '19 at 12:47
  • Yes, and there are filters inside WordPress that can sometime interfere with date time based functions. There are also other standards that should be applied to fit around the framework you are using. – Frits Jul 11 '19 at 12:58
  • First of all, date_format does not use locale. output is always in english, so you need to use another method – ts. Jul 16 '19 at 15:50
  • May be your server has no French locale installed, you can check it by runnung this code - – Artur Babyuk Jul 19 '19 at 13:25

6 Answers6

4

You should use an intl extension:

$ts = new DateTime();
$formatter = new IntlDateFormatter('fr_FR', IntlDateFormatter::LONG, IntlDateFormatter::LONG);
$formatter->setPattern('MMMM d  HH:mm');

echo $formatter->format($ts);

Disponible formats for setPattern are listed here

ts.
  • 10,510
  • 7
  • 47
  • 73
  • The page is is throwing an error on this line `echo $formatter->format($ts);` – flash Jul 16 '19 at 16:14
  • Its a wordpress website. It is displaying `Error Rendering Page`. But its working fine till this line `$formatter->setPattern('MMMM d HH:mm');` – flash Jul 16 '19 at 19:21
  • As soon as I use this `echo $formatter->format($ts);` its not working. – flash Jul 16 '19 at 19:25
3

Use strftime : http://php.net/manual/en/function.strftime.php

<?php
setlocale(LC_TIME, "fr_FR");
echo strftime(" in French %d.%M.%Y and");

(not sure about that %d.%M.%Y but you can read documentation)

OR use Carbon package

Localization http://carbon.nesbot.com/docs/#api-localization

Carbon::setLocale('fr');
Carbon::yesterday()-> diffForHumans();
nima
  • 502
  • 5
  • 14
2

Assuming your website's base language is French, you can make use of the built-in WordPress function date_i18n() like this:

echo date_i18n( 'F j  H:i', $ts ); //Assuming $ts is your time stamp

If $ts is actually a date object, you will have to grab the timestamp first like this:

echo date_i18n( 'F j  H:i', $ts->getTimeStamp() ); //Assuming $ts is a dateTime object

If you are struggling with a timezone difference (i.e. the time returned is a few hours ahead/behind) then you will need to combine the function with get_date_from_gmt like this:

$correct_date = get_date_from_gmt(date_format($ts, 'Y-m-d H:i:s')); //gets the date in your current timezone
echo date_i18n( 'F j  H:i', strtotime($correct_date) );
Frits
  • 7,341
  • 10
  • 42
  • 60
  • What changes I need to make here ? `echo esc_html(date_format($ts, 'F j H:i'))` – flash Jul 11 '19 at 09:44
  • `echo date_i18n(esc_html(date_format($ts, 'F j H:i')))` ? Let me know. – flash Jul 11 '19 at 09:44
  • @flash you can replace that line either with `echo date_i18n( 'F j H:i', $ts );` or with `echo esc_html(date_i18n( 'F j H:i', $ts ));` – Frits Jul 11 '19 at 09:45
  • Its returning blank. – flash Jul 11 '19 at 09:51
  • @flash can you confirm that `$ts` is outputting an actual timestamp? If you replace your code with `echo date_i18n( 'F j H:i', '1562838798' );` does it output todays date? – Frits Jul 11 '19 at 09:53
  • @flash also - can you confirm that your website "Site Language" inside your General Settings has been set to French? – Frits Jul 11 '19 at 09:56
  • `echo date_i18n( 'F j H:i', '1562838798' );` returns `juillet 11 09:53` – flash Jul 11 '19 at 09:57
  • @flash then there is possibly something wrong with your `$ts` variable? – Frits Jul 11 '19 at 10:06
  • On doing `print_r($ts)`, it returns `DateTime Object ( [date] => 2019-07-10 16:00:00.000000 [timezone_type] => 3 [timezone] => America/New_York )` – flash Jul 11 '19 at 10:11
  • @flash that means that `$ts` is a date object, not a timestamp. You should be able to replace your code then with `echo date_i18n( 'F j H:i', $ts->getTimeStamp() );` – Frits Jul 11 '19 at 10:15
  • Sorry for the delay. Its not pulling the right time. Although, its pulling the right day. – flash Jul 11 '19 at 12:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196322/discussion-between-frits-and-flash). – Frits Jul 11 '19 at 12:37
1

Reference from php doc

Note: The return value of setlocale() depends on the system that PHP is running. It returns exactly what the system setlocale function returns.

setlocale(LC_ALL, 'fr_FR');

echo strftime("%A %e %B %Y", mktime(0, 0, 0, 12, 22, 1978)); //vendredi 22 d�cembre 1978
hs-dev2 MR
  • 214
  • 1
  • 12
0

goes around the issue:

<?php 
if (date("m", time() ) === "01" ) { print "Janvier"; }
if (date("m", time() ) === "02" ) { print "Février"; }
if (date("m", time() ) === "03" ) { print "Mars"; }
if (date("m", time() ) === "04" ) { print "Avril"; }
if (date("m", time() ) === "05" ) { print "Mai"; }
if (date("m", time() ) === "06" ) { print "Juin"; }
if (date("m", time() ) === "07" ) { print "Juillet"; }
if (date("m", time() ) === "08" ) { print "Août"; }
if (date("m", time() ) === "09" ) { print "Septembre"; }
if (date("m", time() ) === "10" ) { print "Octobre"; }
if (date("m", time() ) === "11" ) { print "Novembre"; }
if (date("m", time() ) === "12" ) { print "Décembre"; }
?>

I tried to use the setlocale() process and had the same problem you did. The printout was still in English. This might be an 'old school' solution, but it's simple, and it works.

elbrant
  • 763
  • 5
  • 16
  • `date()` will never listen to locales. `strftime()` will, however. :) – Qirel Jul 19 '19 at 07:53
  • @Qirel I tried strftime() along with setlocale() as suggested in [the PHP Manual](https://php.net/manual/en/function.setlocale.php). I had the same issues as the OP, it did not work. This, will. – elbrant Jul 20 '19 at 16:38
0

From http://php.net/manual/en/function.date.php:

To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().