How to pull date/time in french format for wordpress post?

I am working on a wordpress post in which I want the date/time in frech format as shown below:

ENGLISH

By FJ
Published April 9, 2019 at 4:05 p.m.
Last updated April 9, 2019 at 4:14 p.m.


FRENCH

Un texte de FJ
Publié le 9 avril 2019 à 16 h 05
Mis à jour le 9 avril 2019 à 16 h 14

I have used the following code to pull the date/time in english/french for wordpress post:

strong?php if(ICL_LANGUAGE_CODE=='en'){ ?
By
?php }else{ ?
Un texte de
?php  } ??php the_author(); ? /strongbr
strong?php if(ICL_LANGUAGE_CODE=='en'){ ?
Published
?php }else{ ?
Publié le
?php  }  ??php the_time('F j, Y'); ? ?php if(ICL_LANGUAGE_CODE=='en'){ ?
at
?php }else{ ?
à
?php  }  ? ?php the_time('g:i a'); ? /strongbr
strong?php if(ICL_LANGUAGE_CODE=='en'){ ?
Last updated
?php }else{ ?
Mis à jour le
?php  } ??php the_modified_time('F j, Y'); ? ?php if(ICL_LANGUAGE_CODE=='en'){ ?
at
?php }else{ ?
à
?php  }  ? ?php the_modified_time('g:i a'); ? /strong

The above code displays the following o/p which is not correct(date and time format) in the case of french. English is fine but french doesn't seem to work.

By FJ
Published April 9, 2019 at 4:05 p.m.
Last updated April 9, 2019 at 4:14 p.m.

Un texte de FJ
Publié le avril 9, 2019 à 4:05
Mis à jour le avril 9, 2019 à 5:14

Topic php date-time posts Wordpress

Category Web


You need to alter the format of the date/time based on language. This is done by altering the parameters in the_time function. See below for change options. Put the code into the if statement (as an example):

<?php if(ICL_LANGUAGE_CODE=='en'){ ?>
    Last updated <?php the_modified_time('F j, Y'); ?>
    at
<?php }else{ ?>
    Mis à jour le <?php the_modified_time('j F Y'); ?>
    à
<?php  } ?> 

You could make it even simpler by adding the time in the same function rather than calling each 2x. This will save time and space.

<?php if(ICL_LANGUAGE_CODE=='en'){ ?>
    Last updated <?php the_modified_time('F j, Y /a/t g:i a'); ?>
    at
<?php }else{ ?>
    Mis à jour le <?php the_modified_time('j F Y /à h /h i a'); ?>
<?php  } ?> 

I've only shown for modified but you can remove modified_ above and it will work for the creation date.

Notice for the french I changed g to h to move to 24 hour time.

See here for more info:

https://codex.wordpress.org/Formatting_Date_and_Time

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.