How to know if a date/time string contain day or not in php?

Solution:

The DateTime::createFromFormat method tests well that the format is adhered to. Entries like "2019-02-" or "2019-xx-23" are also recognized as incorrect.

$date = "2021-02-x";

$dateTime = DateTime::createFromFormat('Y-m-d',$date);

if($dateTime){
  echo $date.' ok';
}
else {
  echo 'wrong date '.$date;
}

Answer

Solution:

I think you are expecting some thing like this.

<?php
$date1 = "2021-02";
$date2 = "2021-02-11";
$date3 = "2021-12";
$date4 = "2021-12-14";
$date1_array = explode("-", $date1);
$date2_array = explode("-", $date2);
$date3_array = explode("-", $date3);
$date4_array = explode("-", $date4);

if (count ($date1_array) == 3)
{
    echo $date1 . ": It's a Date.";
    echo "<br />";
}
if (count ($date2_array) == 3)
{
    echo $date2 . ": It's a Date.";
    echo "<br />";
}
if (count ($date3_array) == 3)
{
    echo $date3 . ": It's a Date.";
    echo "<br />";
}
if (count ($date4_array) == 3)
{
    echo $date4 . ": It's a Date.";
    echo "<br />";
}

?>

Source