Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PTNOP's avatar
Level 1

Carbon check date

Hello i need to check if a date is in format dd-mm-yyyy

i am using carbon and doing something like this:

I recibe the date from $request->date in this exemple i use the date 10-05-19901

$date = '10-05-19901';

if(Carbon::createFromFormat('d-m-Y', $date) !== false){ ... }

it shows error: Trailing data

The goal is to try check if is valid or not!

Thanks!

0 likes
7 replies
ep!sode's avatar
$request->validate([
      'date' => 'date_format:d-m-Y',
 ]);
PTNOP's avatar
Level 1

but i want do something like

 if ( !empty($date) || check if date is valid format ){
     ....
     }

Thats the reason for carbon i think! Thanks.

ep!sode's avatar
$request->validate([
      'date' => 'nullable|date_format:d-m-Y',
 ]);

Add nullable?

PTNOP's avatar
Level 1

Sorry it was my fault not put all what i want lol sorry! The goal is doing this

         $date = $request->date

     if ( !empty($date) || ('check if $date is valid format') ){
          $setDate = true;
        }else{
           $setDate = false;
           }

i need to pass this parameters!

Thanks again!

1 like
PTNOP's avatar
Level 1

But do you know any solution?To check if is valid format? thanks in advance

ep!sode's avatar
$setDate = false;

$request->validate([
      'date' => 'nullable|date_format:d-m-Y',
 ]);

if ($request->filled('date')) {
	$setDate = true;
}

Try this approach.

Please or to participate in this conversation.