1

Let's say I want to change April 5, 2019 into 2019. április 5 which is from English to Hungarian language.

FYI my date value is automatically grabbed using date('F d, Y', strtotime($row['release_date'])) from a website as the data source

Strftime is not working for me because it's only changing the local time.

I've tried this way but it's not working, it's still displaying the original language

setlocale(LC_TIME, "HU");
utf8_encode(strftime(date('F', strtotime($row['release_date']))));
A J
  • 3,970
  • 14
  • 38
  • 53
  • Hi, and welcome to stack overflow! [This answer](https://stackoverflow.com/a/11435664/10957435) should be helpful and what you need. The only thing that's throwing me is the different language thing. Not sure how this changes. –  Apr 06 '19 at 06:24
  • I saw this comment in PHP docs https://www.php.net/manual/ro/function.setlocale.php#73347 – Scuzzy Apr 06 '19 at 21:58
  • @Chipster thanks mate, but that thread is talking about converting date format, not changing the language. – petresia laura Apr 07 '19 at 02:41
  • @Scuzzy thanks mate, but Control Panel->International Settings doesn't work too, besides I need to keep that setting on English US for my .csv files to work perfectly – petresia laura Apr 07 '19 at 02:42
  • We can use an array. first get the output from the ```date('F d, Y', strtotime($row['release_date']));``` than search for a key like ```April``` on an array, if the key ```April``` found than convert it to ```április```. of course we will need to make list of month name for each language as an array – petresia laura Apr 07 '19 at 02:30

2 Answers2

1

You can also take a look at the The IntlDateFormatter class.

Date Formatter is a concrete class that enables locale-dependent formatting/parsing of dates using pattern strings and/or canned patterns.

This class represents the ICU date formatting functionality. It allows users to display dates in a localized format or to parse strings into PHP date values using pattern strings and/or canned patterns.

My main reason for it: Each system is different and not all locales may be supported by your distribution of PHP and server operating system which makes setlocale pretty much useless for less used locales.

DigiLive
  • 1,093
  • 1
  • 11
  • 28
0

Try the following solution to get date in Hungarian language.

setlocale(LC_TIME, "hu_HU");
echo utf8_encode(strftime('%B %d, %Y', strtotime($row['release_date'])));

If you still get the same result, then this must be a locale problem.

To resolve this, install locale on your machine by following the procedure mentioned here if you have Ubuntu server (I am assuming HU here).

  1. check which locales are supported:

    locale -a

  2. add the locales you want (for example hu):

    sudo locale-gen hu_HU

  3. run this update command

    sudo update-locale

Community
  • 1
  • 1
A J
  • 3,970
  • 14
  • 38
  • 53
  • Instead of `utf8_encode`, you should choose a UTF-8 locale, e.g. `hu_HU.UTF-8`. – deceze Apr 06 '19 at 07:28
  • Thanks for your help but it doesn't work, I think the main problem is on my machine, I'm using windows. – petresia laura Apr 07 '19 at 02:25
  • @petresialaura If there is only one language you want the date in, I think you can use an array for that. If you confirm, I can help you achieve that. – A J Apr 07 '19 at 03:26
  • @AJ let me get this problem straighten. First I got the ```release_date``` data from ```strtotime($row['release_date'])``` which is in yyyy-mm-dd format like ```2019-03-27```, then I use ```date( 'F d, Y'``` to change the format into ```March 27, 2019``` this whole process give me a result in English language and now I want to convert it into other language such as Hungarian. but anyway, please throw me your suggestion may be I can use it to solve my problem – petresia laura Apr 07 '19 at 12:19