title: Error when trying to format dates using an array,PHP
body: <p>I have an array of dates to which I would like to give the format 'Y-m-d', however when I try, I get the error</p>
  
  <p>"Call a member function format () on array"</p>
  
  <p>What I have done before is to pass two dates to a function that returns the time intervals, for example, if I give the following two dates:</p>
  
  <pre><code>2017-06-05 | 2017-06-10
  </code></pre>
  
  <p>This function returns me in Y-m-d format</p>
  
  <pre><code>"2017-06-06,2017-06-07,2017-06-08 ...
  </code></pre>
  
  <p>I previously had no problems formatting the result, however, now that I have sent an array it has stopped working, this is my code</p>
  
  <pre><code>      public function testArray(Request $request)
            {
              if ($request!="" &amp;&amp; $request-&gt;idUser!="") 
              {
               $timeslot= new Timeslot;
               $idUser=$request-&gt;idUser;
               $fecha1=$request-&gt;fecha2;//array
               $fecha2=$request-&gt;fecha1;//array
  
               $slotD=$request-&gt;slotH;//array
               $i=0;
               $k=0;
               $j=0; 
               foreach ($fecha1 as $key =&gt; $value) {
                  $date1[$k]=date('Y-m-d', strtotime($value));
                $k++;
               }
               foreach ($fecha2 as $key =&gt; $value) {
                 $date2[$i]=date('Y-m-d', strtotime($value));
                $i++;
               }
  
  
               foreach ($slotD as $key =&gt; $value) {
                  $value=str_replace("minutos","minutes",$value);
                  $slot[$j]=$value;
                 $j++;
               }
  
               $array=array("fechaInicio"=&gt;$date1,"fechaFin"=&gt;$date2,"slot"=&gt;$slot);
  
               for($l=0;$l&lt;count($date1);$l++)
               {
                $datePeriod["periodo"][]=$timeslot-&gt;returnDates($date1[$l],$date2[$l]);//this method give me an array with date intervals
                  }
               $dates=array();
               $m=0;
  
               foreach ($datePeriod as $key =&gt; $value)
                {
  
                  $dates[$m][$key]=$value-&gt;format('Y-m-d');//ERROR
  
                  $m++;
  
                }
  
  
  
  
  
  
  
            }
  
             return $dates; 
            }
  </code></pre>
  
  <p>The function returnDates is this</p>
  
  <pre><code>function returnDates($fromdate, $todate) {
                      $fromdate =  DateTime::createFromFormat('Y-m-d', $fromdate);
                      $todate =  DateTime::createFromFormat('Y-m-d', $todate);
                      return new   DatePeriod(
                          $fromdate,
                          new  DateInterval('P1D'),
                          $todate-&gt;modify('+1 day')
                      );
                  }
  </code></pre>
  
  <p>When I use the variable $ datePeriod as a common variable, I get the result that I want</p>
  
  <pre><code>for($l=0;$l&lt;count($date1);$l++)
     {
      $datePeriod=$timeslot-&gt;returnDates($date1[$l],$date2[$l]);
        }
  
  
  
  
  Result:
      [["2017-06-17"],{"1":"2017-06-18"},{"2":"2017-06-19"},{"3":"2017-06-20"},{"4":"2017-06-21"}]
  </code></pre>
  
  <p>The problem appears when I convert the variable into an associative array, what could I do in this case?</p>
  
  <p>This is the array that I get if I did not format the dates</p>
  
  <pre><code>[{"periodo":[{"start":{"date":"2017-06-17 18:20:35.000000","timezone_type":3,"timezone":"UTC"},"current":null,"end":{"date":"2017-06-22 18:20:35.000000","timezone_type":3,"timezone":"UTC"},"interval":{"y":0,`"m":0,"d":1,"h":0,"i":0,"s":0,"weekday":0,"weekday_behavior":0,"first_last_day_of":0,"invert":0,"days":false,"special_type":0,"special_amount":0,"have_weekday_relative":0,"have_special_relative":0},"recurrences":1,"include_start_date":true}
          ,{"start":{"date":"2017-06-17 18:20:35.000000","timezone_type":3,"timezone":"UTC"},"current":null,"end":{"date":"2017-06-22 18:20:35.000000","timezone_type":3,"timezone":"UTC"},"interval":{"y":0,"m":0,"d":1,"h":0,"i":0,"s":0,"weekday":0,"weekday_behavior":0,"first_last_day_of":0,"invert":0,"days":false,"special_type":0,"special_amount":0,"have_weekday_relative":0,"have_special_relative":0},"recurrences":1,"include_start_date":true},{"start":{"date":"2017-06-17 18:20:35.000000","timezone_type":3,"timezone":"UTC"},"current":null,"end":{"date":"2017-06-22 18:20:35.000000","timezone_type":3,"timezone":"UTC"},"interval":{"y":0,"m":0,"d":1,"h":0,"i":0,"s":0,"weekday":0,"weekday_behavior":0,"first_last_day_of":0,"invert":0,"days":false,"special_type":0,"special_amount":0,"have_weekday_relative":0,"have_special_relative":0},"recurrences":1,"include_start_date":true}]}]
  </code></pre>
  
  <p>*Update</p>
  
  <p>this is the code of my timeslot class</p>
  
  <pre><code>&lt;?php
              namespace App;
  
              use Illuminate\Database\Eloquent\Model;
              use DateTime;
              use DatePeriod;
              use DateInterval;
              use DB;
              class TimeSlot extends Model
              {
                  protected $table = 'timeslots';
                  public $timestamps = false;
  
              function sumarFechas($times) {
                  $minutes=0;
                  // loop throught all the times
                  foreach ($times as $time) {
                      list($hour, $minute) = explode(':', $time);
                      $minutes += $hour * 60;
                      $minutes += $minute;
                  }
  
                  $hours = floor($minutes / 60);
                  $minutes -= $hours * 60;
  
                  // returns the time already formatted
                  return sprintf('%02d:%02d', $hours, $minutes);
              }
  
  
  
              function returnDates($fromdate, $todate) {
                  $fromdate =  DateTime::createFromFormat('Y-m-d', $fromdate);
                  $todate =  DateTime::createFromFormat('Y-m-d', $todate);
                  return new   DatePeriod(
                      $fromdate,
                      new  DateInterval('P1D'),
                      $todate-&gt;modify('+1 day')
                  );
              }
  
              function returnDates($fromdate, $todate) {
                  $fromdate =  DateTime::createFromFormat('Y-m-d', $fromdate);
                  $todate =  DateTime::createFromFormat('Y-m-d', $todate);
                  return new   DatePeriod(
                      $fromdate,
                      new  DateInterval('P1D'),
                      $todate-&gt;modify('+1 day')
                  );
              }
  
  
              function getDispo()
              {
                  $date=$this-&gt;select('slot_date')
                  -&gt;where('user_id','=',$this-&gt;user_id)
                  -&gt;groupBy('slot_date')
                  -&gt;get();
  
                  return $date;
  
              }
  
              function getHoras()
              {
                  $date=$this-&gt;select('slot_date','inicio','fin')
                  -&gt;where('user_id','=',$this-&gt;user_id)
                  -&gt;get();
  
                  return $date;
  
              }
  
              function getHorasId()
              {
                  $date=$this-&gt;select('id','slot_date','inicio','fin','slot_status')
                  -&gt;where('user_id','=',$this-&gt;user_id)
                  -&gt;where('slot_date','=',$this-&gt;fechaD)
                  -&gt;orderBy(DB::raw("STR_TO_DATE(`inicio`, '%l:%i %p')"))
                  -&gt;get();
  
                  return $date;
  
              }
  
              }
  </code></pre>