Help - Search - Members - Calendar
Full Version: [resolved] Translation of date
Codegrrl.com Forums > Script Help > Tutorial Help
Vlad
Hi everybody!
Is there any php function or some hack that allows to translate the date into national language? In my case it is Russian. Because that code and strtotime () gives you the name of month in English.
CODE
$month = date('F');
$month = htmlspecialchars(strip_tags($_POST['month']));
$realtime = strtotime($month."".$date."".$year."".$time);
$sql = "INSERT INTO chronic (time, title, entry) VALUES ('$realtime', '$title', '$entry')";


Thanx
loadx
you are only converting it to a unix timestamp..so you don't need strtotime()

if it comes in as a textual representation then you need to stop that from happening.

The time you will be storing will it be the current time or a specified time from the end user?

Explain your app and i can point you in the right direction.
Vlad
The only thing I want is to make date displaying in Cyrrilic (Russian) symbols
For example here: http://www.gviragon.ru/chronicle.php I've got some kind of Thursday January 01 1970

The way I'm formating data from DB is like in the tutorial:

$sql = "SELECT * FROM chronic ORDER by id DESC LIMIT 5";
$result = mysql_query($sql) or print ("Что-то с летописью, заклинания не хотят работать.<br />".$sql."<br />".mysql_error());
while ($row = mysql_fetch_array($result)) {
$date = date("l F d Y", $row['time']);

And than printing:
<? echo $date; ?>
Should I somehow use setlocale () function? So will the line
setlocale(LC_ALL, 'rus_RUS');
in the beggining of the code help to convert names of the months into Russian?
loadx
yeah if you are just printing the date then setlocale will do the trick.

I would probably go with:
CODE

setlocale (LC_ALL, 'ru_RU.CP1251');


After a quick google it looks as if that should take care of most russian characters.

You can also supply an array of locale's. So if you have shell access you can run in
CODE
php echo exec('locale -a');


and if you were rolling this out on a bo where you dont know the installed locale's you can pass the ones relevant. e.g
CODE
setlocale(LC_ALL, array('a','b','c'));


Which would run through each element in the array until it finds one that the system has installed.
Amelie
Please note that locales only work if your host has them installed, so if you find that strftime/date/etc. still give you English dates, you may need to ask your host to install the Russian locale onto their server. I found this out when trying to translate dates into French - my server did not have the French language locale installed sad.gif
Vlad
I have added the line setlocale (LC_ALL, 'ru_RU.CP1251'); into the code, but it seems not to work

CODE

<? $page_title = 'Летопись школы';
include('header.php');
setlocale (LC_ALL, 'ru_RU.CP1251');
?>


<h2>Летопись школы магии Гвирагон</h2>
<?

include ('connection.php');
$sql = "SELECT * FROM chronic ORDER by id DESC LIMIT 5";
$result = mysql_query($sql) or print ("Что-то с летописью, заклинания не хотят работать.<br />".$sql."<br />".mysql_error());
while ($row = mysql_fetch_array($result)) {
$date = date("l F d Y", $row['time']);
$title = stripslashes($row['title']);
$entry = stripslashes($row['entry']);
$id = $row['id'];
echo "<p>";
?>
<strong><? echo $title; ?></strong><br /><br />
<? echo $entry; ?><br /><br />
Вехо было создано <? echo $date; ?>
<? $result2 = mysql_query("SELECT id FROM chronic_cmt WHERE entry='$id'");
$num_rows = mysql_num_rows($result2);
if ($num_rows > 0) {
    echo "<a href=\"entry.php?id=" . $id . "\">   мнений: " . $num_rows . "</a>";
}
else {
    echo "<a href=\"entry.php?id=" . $id . "\">  Высказать своё мнение</a>";
}
?>
<hr width="400px">
<?
}
echo "</p>";


include('footer.php'); ?>



Amelie,

Ok, I'll find out it, thanx for advice.
Vlad
On my hosting I have
ru_RU.cp1251
ru_RU.koi8r
ru_RU.utf8
locales installed. But adding setlocale (LC_ALL, 'ru_RU.cp1251'); line doesn't help sad.gif Months are still in English
Amelie
Use strftime() rather than date() - date() doesn't support translated dates, but strftime does.

So, instead of this code:

CODE
$date = date("l F d Y", $row['time']);


Use this:

CODE
$date = strftime("%A %B %d %Y", $row['time']);
Vlad
Amelie, thank you. Resolved smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.