freephoneid Posted January 23, 2009 Share Posted January 23, 2009 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! Quote Link to comment Share on other sites More sharing options...
Mchl Posted January 24, 2009 Share Posted January 24, 2009 strtotime will only work for English Quote Link to comment Share on other sites More sharing options...
freephoneid Posted January 24, 2009 Author Share Posted January 24, 2009 strtotime works for both English & German. Can you please provide me the solution? Thanks in Advance!! Quote Link to comment Share on other sites More sharing options...
Mchl Posted January 24, 2009 Share Posted January 24, 2009 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 Quote Link to comment Share on other sites More sharing options...
freephoneid Posted January 24, 2009 Author Share Posted January 24, 2009 Hi There, Thanks again for the reply. I understand that strtotime works for only specific data items. But I'm still not clear... What is the solution so that I can fire query to database using db format yyyy-mm-dd? Thanks! Quote Link to comment Share on other sites More sharing options...
Mchl Posted January 24, 2009 Share Posted January 24, 2009 You will probably need to create a custom function to parse japanese date format. Should not be that hard. Quote Link to comment Share on other sites More sharing options...
freephoneid Posted January 24, 2009 Author Share Posted January 24, 2009 Can you please provide me some sample code? Reason I'm asking is beacause how to parse the date which is already in Japanese format to DB version which requires yyyy-mm-dd? Thanks! Quote Link to comment Share on other sites More sharing options...
Mchl Posted January 24, 2009 Share Posted January 24, 2009 What is the Japanese format? Quote Link to comment Share on other sites More sharing options...
freephoneid Posted January 24, 2009 Author Share Posted January 24, 2009 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! Quote Link to comment Share on other sites More sharing options...
Mchl Posted January 24, 2009 Share Posted January 24, 2009 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 Quote Link to comment Share on other sites More sharing options...
freephoneid Posted January 24, 2009 Author Share Posted January 24, 2009 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! Quote Link to comment Share on other sites More sharing options...
Mchl Posted January 24, 2009 Share Posted January 24, 2009 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 Quote Link to comment Share on other sites More sharing options...
haku Posted January 24, 2009 Share Posted January 24, 2009 This is why I always just work with unix timestamps, and the date() function. I find it easier to work with dates that way. Quote Link to comment Share on other sites More sharing options...
freephoneid Posted January 26, 2009 Author Share Posted January 26, 2009 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! Quote Link to comment Share on other sites More sharing options...
freephoneid Posted January 26, 2009 Author Share Posted January 26, 2009 Hello, Can someone help me? Thanks! Quote Link to comment Share on other sites More sharing options...
Mchl Posted January 26, 2009 Share Posted January 26, 2009 please deploy the code which I've given earlier in Linux environment. If only I had access to such environment I certainly would. Alas I don't. Quote Link to comment Share on other sites More sharing options...
freephoneid Posted January 26, 2009 Author Share Posted January 26, 2009 okay..Can at least try in windows environment? Don't forget to change the browser locale to Japanese. You would see date as 1970. Quote Link to comment Share on other sites More sharing options...
Mchl Posted January 26, 2009 Share Posted January 26, 2009 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 Quote Link to comment Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 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. Quote Link to comment Share on other sites More sharing options...
freephoneid Posted January 26, 2009 Author Share Posted January 26, 2009 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?? Quote Link to comment Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 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. Quote Link to comment Share on other sites More sharing options...
Mchl Posted January 26, 2009 Share Posted January 26, 2009 There's no way strtotime() could parse this 2009ǯ01·î26Æü I never suggested storing anything as timestamp What I'm wondering is: why do you even need to parse date in local format? Is it user's input? Quote Link to comment Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 There's no way strtotime() could parse this 2009ǯ01·î26Æü I never suggested storing anything as timestamp 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 Quote Link to comment Share on other sites More sharing options...
freephoneid Posted January 26, 2009 Author Share Posted January 26, 2009 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? Quote Link to comment Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.