How to get only the month of a date with Laravel 5.3?

1

I need to get only the month of a date that comes with a $request . But I do not know how to do it:

if ($request->parcelas > 1) {
        $mes = $request->data_vencimento = date('m');
        for($i = 0; $i <= $request->parcelas; $i++) {
            if($request->data_vencimento = date('m') == 12 ){
                $mes = 1;
            }
            var_dump($request->data_vencimento = date('Y-'. $mes . '-d'));
            $mes ++;
        }
    }

The date comes in dd/mm/yyyy format

That way it picks up the current date. Can anyone help me?

    
asked by anonymous 21.11.2016 / 16:42

3 answers

2

Use the DateTime::createFromFormat method to instantiate a DateTime object from the date in the desired format.

See:

$vencimento = \DateTime::createFromFormat('d/m/Y', $request->data_vencimento);

dd($vencimento->format('m'));

That is, in your case, it could look like this:

if ($request->parcelas > 1) {

    $vencimento = \DateTime::createFromFormat('d/m/Y', $request->data_vencimento);

    $mes = $vencimento->format('m');

    // resto do código
}
    
21.11.2016 / 16:55
0

If you are sending the date in yyyy-mm-dd format you can get: date('m', strtotime($request->data_vencimento)) .

    
21.11.2016 / 16:44
0

So you return only the month

date( 'm' , strtotime($request->data_vencimento));
    
21.09.2017 / 20:16