dongzhuo1958 2014-07-08 16:38
浏览 19
已采纳

与宅基地的Laravel strftime无法正常工作

I've been having alot of trouble with formating a date with my localization using Laravel and Homestead (I don't know if homestead has any kinda server configurations which make it doesn't work but thats my though). I guess anyone around the internet have been struggling with the same issue. I've been searching for an answer for hours without getting anywhere closer to a solution.. This is my code.

public function formated_start()
{
    setlocale(LC_ALL, 'sv_SE');
    return strftime('%A %H:%M', strtotime($this->start));
}

I've also tried with laravel specific

public function formated_start()
{
    App::setLocale('sv_SE')
    return strftime('%A %H:%M', strtotime($this->start));
}

Still with the same result nothing changed and the %A which should be the day of week in my language in this case Swedish is still in English.

  • 写回答

3条回答 默认 最新

  • drd0833 2014-07-08 19:16
    关注

    Homestead provides an Ubuntu system. So first check which locales are available on your system with locale -a. Maybe the swedish locale is named sv_SE.iso88591 or sv_SE.utf8. If it is not installed you can install it with:

    sudo locale-gen sv_SE sv_SE.utf8
    

    After installing your first solution with setlocale(); should work. This would be the PHP-way to generate dates.

    Don't forget to restart the server after you've installed the locale's described

    sudo nginx restart
    

    The Laravel-way

    For the Laravel-way you need to generate the locals yourself. Start by adding a locale file in /app/lang/sv/days.php with:

    <?php
    
    return array(
        'Monday' => 'måndag',
        'Tuesday' => 'tisdag',
        'Wednesday' => '..',
        'Thursday' => '..',
        'Friday' => '..',
        'Saturday' => 'lördag',
        'Sunday' => '..',
    );
    

    Now you can use the App::setLocale() with Lang::get():

    App::setLocale('sv');
    echo Lang::get(sprintf('days.%s', date('l')));
    

    This is way more complex but if you are hosting Laravel on a shared hosting environment where you can't generate locales, this could be a possible way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line