Search code examples
phpdatesetlocale

How to use setlocale function in php to get month name in french?


I am working on a php code $formated_date_new = date('M d, Y',strtotime($this_date)); which gives me the month name in english.

<script>
       document.getElementById('title_en').value = "<?php echo $formated_date_new ?>";
</script>

The above script returns the date in the following format (with month name in english) :

Mar 13, 2019 

For getting the month name in french, I need to integrate the following code but I am not sure how to do it.

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


The o/p which I want is:

13 mars 2019

Solution

  • Use strftime instead of date:

    setlocale( LC_TIME, "fr_FR" );
    $formated_date_new = strftime( "%d %b %Y", strtotime( $this_date ) );
    

    strftime refernce:

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