【问题标题】:How to change date language?如何更改日期语言?
【发布时间】:2014-09-29 13:49:49
【问题描述】:
<table class="projects">
    <tr><td class="title" colspan="2">Vergangene Projekte</td></tr>

<?php   $query = "SELECT * FROM posts WHERE project = 1 && project_date <= CAST(CURRENT_TIMESTAMP AS DATE)ORDER BY date DESC";
        if(!$query){
        die("Konnte nicht mit Data Base verbinden");}
        $result = mysqli_query($connection, $query);

while($row = mysqli_fetch_array($result)){

        setlocale(LC_ALL, 'de_DE'); // using german language works
        $premier = new DateTime($row['project_date']);
        echo "<tr><td>" .strftime("%B", $premier->getTimestamp()). "</td><td class=\"project_title\">
        <a class=\"title\" href=\"projekt_info.php?projekt=" .urlencode($row["id"]) ."\">" 
        .$row['title']. "</a></td></tr>";} 
?>
</table>

好的,我修改了代码以显示所有内容并插入了建议,现在它可以正常工作了!!完美

干杯 克里斯

【问题讨论】:

  • 可以试试加setlocale(LC_TIME, 'fr_FR');
  • 另外,这是你的完整代码吗?如果是这样,那么缺少很多东西。表格标签,例如数据获取。
  • 并确保机器中确实安装了fr_FR 语言环境。 locale -a

标签: php html mysql date


【解决方案1】:

首先确保在机器上确实安装了fr_FR 以供您的脚本使用。

尝试在终端上使用locale -a 进行检查。

其次,由于DateTime 仅适用于英文,请在其上使用strftime(),并且您在使用它时没有时间戳。

setlocale(LC_ALL, 'fr_FR');
$premier = new DateTime($row['project_date']);
echo "<tr><td>" .strftime("%B", $premier->getTimestamp()). "</td><tr>";
                                   // ^^ feed the timestamp

旁注:正如 Fred 在 cmets 中所说,这只是一个片段,因为您的标记有点偏离(缺少标签等,很可能这是在循环内)。

【讨论】:

    【解决方案2】:

    有点晚了,但我更喜欢使用独立于系统语言环境的 IntlDateFormatter,并且保证输出为 UTF-8:

    $datefmt = new IntlDateFormatter('de_DE', NULL, NULL, NULL, NULL, 'LLLL');
    while($row = mysqli_fetch_array($result)) {
        # ...
        echo "<tr><td>" . $datefmt->format($premier). "</td>"/*...*/;
        # ...
    

    (请参阅 ICU 的文档以了解 a custom format - IntlDateFormatter 构造函数的最后一个参数)

    【讨论】:

      猜你喜欢
      • 2015-11-12
      • 2018-08-24
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多