ライブラリ「Carbon」を用いて日付を処理する際、表示形式に疑問があった為、そこ部分に関してお聞きしたいです。
フレームワーク(laravel)を使用していますが、今回は質問内容に関係があまりないと思った為、タグは外させて貰っています。
php
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use Carbon\Carbon; 7 8class Reservecontroller extends Controller 9{ 10 // 11 12 13 public function getCalendarDates($year, $month) 14 { 15 $date = Carbon::parse("$year-$month-01")->locale('ja_JP'); 16 // dd($date); 17 $last = $date->copy()->startOfWeek(); 18 $count = $last->diffInDays($date->copy()->endOfMonth()->endOfWeek()) + 1; 19 // dd($count,$date); 20 for ($i = 0; $i < $count; $i++, $last->addDay()) { 21 $dates[] = $last->copy(); 22 } 23 // dd($dates); 24 return $dates; 25 } 26 27 public function reserve_date() 28 { 29 30 $current_date = new Carbon(); 31 $current_year = $current_date->year; 32 $current_month = $current_date->month; 33 $dates = $this->getCalendarDates($current_year,$current_month); 34 return view('reserve.calender',compact('dates','current_date')); 35 36 } 37 38} 39
html
1@php 2 dd($dates); 3 @endphp 4 <table class="calender_table"> 5 <thead> 6 <tr class="day"> 7 @foreach (['日', '月', '火', '水', '木', '金', '土'] as $dayOfWeek) 8 @if($loop->first) 9 <th class="sunday">{{ $dayOfWeek }}</th> 10 @elseif($loop->last) 11 <th class="saturday">{{ $dayOfWeek }}</th> 12 @else 13 <th>{{$dayOfWeek}}</th> 14 @endif 15 @endforeach 16 </tr> 17 </thead> 18 <tbody> 19 @foreach($dates as $date) 20 @if($date->dayOfWeek == 0) 21 <tr> 22 @endif 23 <td> 24 @if($date->month == $current_date->month) 25 {{$date->day}} 26 @endif 27 </td> 28 @if($date->dayOfWeek == 6) 29 </tr> 30 @endif 31 @endforeach 32 </tbody> 33 34 </table> 35
HTMlのコードはtable内の部分のみ抜粋しています。
HTMLコードの方(calendar.blade.php)でdd($dates)として、オブジェクトを確認した結果以下のように格納されていました。
dd()メソッドを取り除き実際のこちらの画面を実行し表示させてみますと以下のようになります。
dd()で確認した際は、日付が、06-01と日にちの部分が「01」となっているのですが、実際にtable内に表示されているのは、「1」となっています。
何故、「01」でなく「1」と表示されてしまうのでしょうか?
追記です
bladeファイルの方でvar_dump($dates)とし確認しましたが、「2019-06-07 00:00:00.000000」こちらのように日にちには確実に、「07」の形式で入っていました。他の日にちに関しても、「01」と2桁で日にちが格納されていました