Validate the month of a timestamp data fix with php and Laravel5.5

0

In my controller I bring the date of an element with created_at this returns an array with the dates in my database, I need to validate if within this data array is the 07th month or not. My problem is that pru brings me the month 1 if I make a dd and if I bring the year it brings me 1970, it makes me validate one of the elements of the circle if in the function strto time in the date array I put an element like dates [ 0] but only one, I need you to validate in all. This is the code that I am implementing:

   public function index()
    {
    $dates = DB::table('stocks')->pluck('created_at');

    foreach($dates as $date){
        $pru = date("m", strtotime($dates)); 
        $pru2 = collect($pru);
        // dd($pru);
        if($pru2->contains('07')){
            dd("si tiene Julio");
        }else{
            dd("no tiene Julio");
        }
    }
    return view('chartjsprueba')->with([
         'stocks', $stocks,
         'fecha', $fecha
         ]);
}

How can you validate the month in all the elements of the $ dates array?

    
asked by Freddy Forero 24.07.2018 в 18:13
source

1 answer

0

A different approach was taken to validate the month of the data requested by the user, a query was made for the model with whereBetween where the start and closing dates for the data requested are entered in a view.

public function show($id)
{
//fechas insertadas de una vista, son tipo date
   $fecha1 = DB::table('fechas')->pluck('fecha_inicio')->all();
   $fecha2 = DB::table('fechas')->pluck('fecha_fin')->all();
//se convierten en string
   $fecha1 = implode($fecha1);
   $fecha2 = implode($fecha2);
//se vuelven un string de carbon
   $primer_dia = (new Carbon($fecha1))->toDateString();
   $ultimo_dia = (new Carbon($fecha2))->toDateString();
//consulta
   $consumes = consume::where('user_id', $id)
      ->whereBetween('created_at',[$primer_dia,$ultimo_dia])
      ->get();
//retorna la vista con estos datos
   return view('user.consumo.show', [
      'consumes'=>$consumes,
      'fecha1' =>$fecha1,
      'fecha2' =>$fecha2,
   ]);
}

It was necessary to convert the date to string in the indicated way since if it was done directly it did not validate the wherebetween. I remain attentive to comments.

    
answered by 27.07.2018 в 23:30