Jump to content

Recommended Posts

Hi,

  I'm using below code to display date in different languages.

 

if ($Language == 'en') {
setlocale(LC_ALL,'en_US');
} else if ($Language == 'ja') {
setlocale(LC_ALL,'ja_JP.utf8');
} else if ($Language == 'de') {
setlocale(LC_ALL,'de_DE');
}

$date = strftime("%x", strtotime("today"));

 

  Now, it works fine & displays date properly in Japanese & German languages. The problems comes when I pass this paramter $date to another function where I need to fire query to database based on this date.

 

   $dbdate = strftime("%Y-%m-%d", strtotime($date));

 

Now, the above does not work for Japanese since the $date input to above code returns 1970-01-01

 

Can anyone tell me how to resolve this issue?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/142191-date-issue-in-japanese-language/
Share on other sites

Sorry...what I wrote is not really relevant in your case...

 

%x in strftime means "preferred date representation for the current locale without the time "

 

Now, strtotime will only work for certain representations of date (GNU Date Input Formats).

http://www.gnu.org/software/tar/manual/html_node/Date-input-formats.html

Thanks again for reply!

 

Japanese Date format is yyyy-mm-dd

 

However, let me revise my question....

 

When I print the below date with browser set to Japanese locale,

 

$dbdate = strftime("%Y-%m-%d", strtotime($date));

 

it returns me 1970-01-01

 

How can parsing help me & what to parse? Can you please be more specific?

 

Thanks!

 

I don't get it...

YYYY-mm-dd should be parsed by strtotime with no problems

 

what's more, to insert to database you need exactly YYYY-mm-dd format... so why even changing it?

 

1970-01-01 basically means that either the format is not right, or that $date is empty

 

Make sure, that the date in $date is indeed in the format you think it is...

 

Anyway, I got to go offline now. Take a look at this function though

http://www.php.net/manual/en/function.date-parse-from-format.php

Here is the complete code which returns me 1970-01-01 for Japanese locale:

 

<?php

$SupportedLanguages = array("en", "ja", "de");
$Language = "en"; // Default

if (isset($_SERVER{'HTTP_ACCEPT_LANGUAGE'})) {
$Lang = $_SERVER{'HTTP_ACCEPT_LANGUAGE'};
$pos = strpos($Lang, ";q=");
if (!$pos===false) list($Lang,$Qvalue) = split(';', $Lang);

$LangList = split(',', $Lang);

foreach ($LangList as $LangItem) {
	list($Lang, $Country) = split('-', $LangItem);

	if (in_array($Lang, $SupportedLanguages)) {
		$Language = $Lang;
		break;
	}
}
}


if ($Language == 'en') {
setlocale(LC_ALL,'en_US');
} else if ($Language == 'ja') {
setlocale(LC_ALL,'ja_JP.utf8');
} else if ($Language == 'de') {
setlocale(LC_ALL,'de_DE');
}

$date = strftime("%x", strtotime("today"));

$dbDate = strftime("%Y-%m-%d", strtotime($date));

print "Date to be displayed in UI: $date  <br \>";

print "Date requred for firing query in DB: $dbDate";
?>

 

Can any one help? The above code prints both UI date & dbDate properly in German & English. But for Japan, it shows 1970-01-01 as dbDate. Can any one tell me where to modify code to display date properly in Japanese locale too????

 

Thanks!

Do you just want to store current date in database?

 

I'm running this on my (windows) PC

 

setlocale(LC_ALL,'en_US', 'american');
   
$date = strftime("%x", strtotime("today"));
$dbDate = strftime("%Y-%m-%d", strtotime($date));
print "Date to be displayed in UI: $date  <br \>";
print "Date requred for firing query in DB: $dbDate<br \>";   

setlocale(LC_ALL,'ja_JP.utf8','japanese');

$date = strftime("%x", strtotime("today"));
$dbDate = strftime("%Y-%m-%d", strtotime($date));
print "Date to be displayed in UI: $date  <br \>";
print "Date requred for firing query in DB: $dbDate<br \>";   
   
setlocale(LC_ALL,'de_DE','german');

$date = strftime("%x", strtotime("today"));
$dbDate = strftime("%Y-%m-%d", strtotime($date));
print "Date to be displayed in UI: $date  <br \>";
print "Date requred for firing query in DB: $dbDate<br \>";

 

and it displays

 

Date to be displayed in UI: 1/24/2009
Date requred for firing query in DB: 2009-01-24
Date to be displayed in UI: 2009/01/24
Date requred for firing query in DB: 2009-01-24
Date to be displayed in UI: 24.01.2009
Date requred for firing query in DB: 2009-01-24

Hi Mchl,

    Thanks again for your reply. However, please deploy the code which I've given earlier in Linux environment. And then try to run it from windows through browser. Don't

forget to set the browser settings to Japanese before running so that it'll pick up Japanese locale.

 

    Then, you'll see that the output for "Date to be displayed in UI: $date  <br \>"; will be properly displayed in Japanese language with their own format yyyy-mm-dd.

However, 2nd line output for "Date requred for firing query in DB: $dbDate"; will be 1970-01-01.

 

I want this 2nd output to display correctly & not 1970.

 

Can you please help???

 

Thanks again!

I run this code

<?php

$SupportedLanguages = array("en", "ja", "de");
$Language = "en"; // Default

if (isset($_SERVER{'HTTP_ACCEPT_LANGUAGE'})) {
   $Lang = $_SERVER{'HTTP_ACCEPT_LANGUAGE'};
   
   $pos = strpos($Lang, ";q=");
   if (!$pos===false) list($Lang,$Qvalue) = split(';', $Lang);
   
   $LangList = split(',', $Lang);
   
   foreach ($LangList as $LangItem) {
      list($Lang, $Country) = split('-', $LangItem);
   
      if (in_array($Lang, $SupportedLanguages)) {
         $Language = $Lang;
         break;
      }
   }
}

echo $Language. "<br>\n";
if ($Language == 'en') {
   setlocale(LC_ALL,'en_US','american');
} else if ($Language == 'ja') {
   setlocale(LC_ALL,'ja_JP.utf8','japanese');
} else if ($Language == 'de') {
   setlocale(LC_ALL,'de_DE','german');
}

$date = strftime("%x", strtotime("today"));

$dbDate = strftime("%Y-%m-%d", strtotime($date));

print "Date to be displayed in UI: $date  <br \>";

print "Date requred for firing query in DB: $dbDate";
?>

 

And get

 

ja
Date to be displayed in UI: 2009/01/26
Date requred for firing query in DB: 2009-01-26

 

If I remove Windows specific locales ('american','german','japanese') I get

 

ja
Date to be displayed in UI: 01/26/09
Date requred for firing query in DB: 2009-01-26

check /usr/share/locale/ if you want more info about the locale available with your *NIX box

there is also a file called /usr/share/locale/locale.alias with a list of aliases

such as swedish for sv_SE

so on all boxes i have accounts on (rh 6.0 and slack 3.4) you can just use setlocale("LC_ALL","swedish"); or other prefered language in plain english.

However, the weekdays were in all lowercase :(

Note: export LC_ALL=swedish made a lot of programs swedish for me, it's also possible to make them russian or japanese :)

 

I found this online in the php.net man notes for setlocale.

 

Check that folder to see if you have the japanese language in there. If it is not there, I do not think you can use it.

Hi,

    I found that we cannot replicate this issue in Windows. The UI date does not get displayed in Japanese within Windows environment.

 

I checked /usr/share/locale & I've everything installed properly. Since my web site is already up & running in Japanese. The issue is coming only when I fetch the date.

 

I'm getting below display for Japanese.

 

ja
Date to be displayed in UI: 2009ǯ01·î26Æü 
Date requred for firing query in DB: 1970-01-01 

 

Here, the UI date is printed properly. However, the DB date is not getting displayed properly & always shows 1970.

 

Any suggestions??

Well I am not sure on working with Japanese and DB, but does strtotime actually take in a Japanese date? From the sounds of it that is where your problem is and I would suggest taking mchl's advise and storing the date in a timestamp so you can just use strftime to display it without having the need to use strtotime

 

That is just my 2 cents.

There's no way strtotime() could parse this

2009ǯ01·î26Æü 

 

I never suggested storing anything as timestamp :P What I'm wondering is: why do you even need to parse date in local format? Is it user's input?

 

lol my bad, been a long day and my mind is going to the pits and creating stuff that is not there ;) ....damn those gremlins in my keyboard.....

 

EDIT:

Change mchl to haku in the above post...I knew I saw that somewhere on here!

 

EDIT: EDIT:

Thinking along mchl's line, why do you care if it is stored in the DB as Japanese time, store it as regular time then on page display use the strftime and display it as Japanese....I finally got what mchl was trying to say :)

Well, the code above is just a sample code which I've given here....What I'm doing is that I'm

generating date from today till last 7 days in a loop in their repspective locale & while doing

that I'm using %x which takes care of displaying date in respective locale.

 

This takes care of displaying 7 dates as column header in my page properly.

 

Now, next is to get some data from the backend postgres for all these dates.

 

Since the date is initially parsed using %x, when I try the strftime function on that variable,

it always returns me 1970

 

$date = strftime("%x", strtotime("today"));
$dbDate = strftime("%Y-%m-%d", strtotime($date));

 

Above code is placed in a loop where I'm generating 7 days. And it works properly in all other

languages except Japanese.

 

In Japanese strftime("%Y-%m-%d", strtotime($date)) is always returning me 1970-01-01

 

That's why I gave this sample code to show you that it always prints 1970 year in Japanese

whereas in other languages, it prints proper date.

 

Any suggestions?

 

 

strtotime — Parse about any English textual datetime description into a Unix timestamp

 

strtotime

 

It has to be english, chances are the other languages kept the numbers, where as Japanese uses characters for the numbers, thus strtotime will not work if the time has already been converted to Japanese.

 

Simple as that.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.