LoginSignup
1
0

More than 3 years have passed since last update.

Laravelで日付をYYYY年MM月DD日 (曜日) hh: mmと表示させる

Posted at

日付と時間だけだと簡単ですが、間に曜日を挟んだ場合の情報が少なくて面倒だったので、メモ
LaravelにCarbonのv2以降をインストールすれば以下のように1行でかけます。
今回は参考にdirectiveを使ってますが、viewHelperとかの方がいいかも。

AppServiceProvider.php
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        \Blade::directive('datetime_and_week', function ($expression) {
            return "<?php echo Carbon\Carbon::parse($expression)->isoFormat('YYYY年MM月DD日 (ddd) LT '); ?>";
        });
    }
}

directiveなのでView clearコマンドを実行後に
viewなどで下記のように呼び出せばOK

Sample.blade.php
<?php

@datetime_and_week('2019-11-24 00:00:00')

下記のように表示されます!
2019年11月24日 (日) 00:00

他に良い方法あれば教えてください。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0