-1

I am working on a php code below which displays the month in php.

<script>
document.getElementById('title_fr').value = "<?php setlocale(LC_TIME, "frc"); echo strftime("%d %B %Y", strtotime( $this_date )); ?>"; 
</script>

The above php code returns month name in french as showm below.

janvier
février
mars
avril
mai
juin
juillet
août
septembre
octobre
novembre
décembre

Problem Statement:

I am wondering what changes I need to make in the php code above so that For days of the month, if d = “1” then substitute “1er”

I think I need to make some changes here echo strftime("%d %B %Y", strtotime( $this_date )); but I am not sure where exactly.

I added er after %d but it has added er after every day of the month.

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
john
  • 11,311
  • 40
  • 131
  • 251

1 Answers1

1

Get the day number, and if it's 1 then append er.

<?php
setlocale(LC_TIME, "frc");
$this_time = strtotime( $this_date );
$day = date('d', $this_time);
$suffix = $day == 1 ? 'er' : '';
$formatted = strftime("%d$suffix %B %Y", $this_time);
?>
document.getElementById('title_fr').value = "<?php echo $formatted; ?>"; 
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks, it worked. I have one more [question](https://stackoverflow.com/questions/55166190/ugly-characters-are-returned-for-months-f%C3%A9vrier-and-d%C3%A9cembre-in-html-php?noredirect=1#comment97073919_55166190), its not similar but I am still figuring out why its returning ugly characters. – john Mar 14 '19 at 17:29
  • Although, its marked as duplicate but the answer didn't help me much. – john Mar 14 '19 at 17:35
  • You need to set the HTML character set. https://stackoverflow.com/questions/4696499/meta-charset-utf-8-vs-meta-http-equiv-content-type – Barmar Mar 14 '19 at 18:26