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
"Call a member function format () on array"
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:
2017-06-05 | 2017-06-10
This function returns me in Y-m-d format
"2017-06-06,2017-06-07,2017-06-08 ...
I previously had no problems formatting the result, however, now that I have sent an array it has stopped working, this is my code
public function testArray(Request $request)
{
if ($request!="" && $request->idUser!="")
{
$timeslot= new Timeslot;
$idUser=$request->idUser;
$fecha1=$request->fecha2;//array
$fecha2=$request->fecha1;//array
$slotD=$request->slotH;//array
$i=0;
$k=0;
$j=0;
foreach ($fecha1 as $key => $value) {
$date1[$k]=date('Y-m-d', strtotime($value));
$k++;
}
foreach ($fecha2 as $key => $value) {
$date2[$i]=date('Y-m-d', strtotime($value));
$i++;
}
foreach ($slotD as $key => $value) {
$value=str_replace("minutos","minutes",$value);
$slot[$j]=$value;
$j++;
}
$array=array("fechaInicio"=>$date1,"fechaFin"=>$date2,"slot"=>$slot);
for($l=0;$l<count($date1);$l++)
{
$datePeriod["periodo"][]=$timeslot->returnDates($date1[$l],$date2[$l]);//this method give me an array with date intervals
}
$dates=array();
$m=0;
foreach ($datePeriod as $key => $value)
{
$dates[$m][$key]=$value->format('Y-m-d');//ERROR
$m++;
}
}
return $dates;
}
The function returnDates is this
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->modify('+1 day')
);
}
When I use the variable $ datePeriod as a common variable, I get the result that I want
for($l=0;$l<count($date1);$l++)
{
$datePeriod=$timeslot->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"}]
The problem appears when I convert the variable into an associative array, what could I do in this case?
This is the array that I get if I did not format the dates
[{"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}]}]
*Update
this is the code of my timeslot class
<?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->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->modify('+1 day')
);
}
function getDispo()
{
$date=$this->select('slot_date')
->where('user_id','=',$this->user_id)
->groupBy('slot_date')
->get();
return $date;
}
function getHoras()
{
$date=$this->select('slot_date','inicio','fin')
->where('user_id','=',$this->user_id)
->get();
return $date;
}
function getHorasId()
{
$date=$this->select('id','slot_date','inicio','fin','slot_status')
->where('user_id','=',$this->user_id)
->where('slot_date','=',$this->fechaD)
->orderBy(DB::raw("STR_TO_DATE(`inicio`, '%l:%i %p')"))
->get();
return $date;
}
}
It sounds like $value
is not an instance of object DateTime
. Try replacing this line:
$dates[$m][$key]=$value->format('Y-m-d');//ERROR
with these two:
$timestamp = strtotime($value);
$dates[$m][$key]=date('Y-m-d',$timestamp);//NO ERROR? :-)
You can also combine it into one line:
$dates[$m][$key]=date('Y-m-d',strtotime($value));//NO ERROR? :-)
formatting the date before parsing it should do the trick
$value['date'] = date('Y-m-d',strtotime($value['date']));
$dates[$m][$key]=$value;
//This returns the array with the date formatted
OR
$dates[$m][$key]=date('Y-m-d',strtotime($value['date']));
//This returns just the formatted dates