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

Asked

Viewed 1,420 times

1

I need to take just the month of a date that comes by a $request. But I don’t 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 ++;
        }
    }

Date comes in format dd/mm/yyyy

That way he gets the current date. Someone can help me?

  • In what shape it comes?

  • 1

    @Virgilionovic dd/mm/yyyy

3 answers

2


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

Behold:

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

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

I mean, 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
}

0

So also you return only the month

date( 'm' , strtotime($request->data_vencimento));

0

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.