0

What I'm trying to do is display the local date using PHP. Right now I have a javascript solution that prints the month that corresponds with the php variable for the month in whatever language I want. For example for <?php echo $one; ?> (January) it can be populated with January (english), Enero (spanish), or Gennaio (italian).

Is there any way to convert this bit of code completely to PHP? if not, is there any way I can turn the code into a php variable so I can just put something like <?php echo $date; ?> and still define $one, $two, $three, etc?

<script type="text/javascript">
var month = new Array();
month[0] = "<?php echo $one; ?>";
month[1] = "<?php echo $two; ?>";
month[2] = "<?php echo $three; ?>";
month[3] = "<?php echo $four; ?>";
month[4] = "<?php echo $five; ?>";
month[5] = "<?php echo $six; ?>";
month[6] = "<?php echo $seven; ?>";
month[7] = "<?php echo $eight; ?>";
month[8] = "<?php echo $nine; ?>";
month[9] = "<?php echo $ten; ?>";
month[10] = "<?php echo $eleven; ?>";
month[11] = "<?php echo $twelve; ?>";

//Array starting at 0 since javascript dates start at 0 instead of 1
var mydate= new Date()
mydate.setDate(mydate.getDate())
document.write(""+month[mydate.getMonth()]+" "+mydate.getDate()+", "+mydate.getFullYear());
</script>

Sorry for not having a fiddle, I would create one but since there is some php involved I'm not sure of where to go for that. If there is something like jsfiddle that also includes php ill set that up if needed so its much easier to understand.

Joe Bobby
  • 2,803
  • 10
  • 40
  • 59
  • This seems like an extraordinarily overcomplicated and fragile way of doing it. Consider http://momentjs.com/ and its locale support. – ceejayoz Dec 09 '14 at 21:40
  • @ceejayoz im only targeting 1 country at a time and since some countries have multiple languages i need control over which language is displayed – Joe Bobby Dec 09 '14 at 22:21
  • @machei im not sure what your response means – Joe Bobby Dec 09 '14 at 22:21

1 Answers1

0

In PHP, you can use setlocale() along with strftime(). So, for example if you want to print the current month in French, you could do:

setlocale(LC_TIME, "fr_CA");
echo strftime("%B");

List of Locales: List of All Locales and Their Short Codes?

Community
  • 1
  • 1
jdmcguire
  • 111
  • 3
  • been lokoing through the strftime() link. Is there no way to say `December 9, 2014`? closest thing I see is `%x` – Joe Bobby Dec 09 '14 at 22:36
  • `echo strftime("%B %e, %Y");` – Mikey Dec 09 '14 at 23:36
  • @Mikey some countries put the day before the month and `%x` will display those countries in that format instead of `Month Day, Year`.. so I'm looking for a way to do this but keep correct local format – Joe Bobby Dec 10 '14 at 00:30