Todd Moses
Todd Moses

Reputation: 11029

How to determine if value is a date in PHP

I am working with arrays of values in PHP. Some of these values may include a date in various string formats.

I need to convert dates in multiple formats to their numerical equivalent (Unix timestamp). The problem is being able to determine if the string is a date.

Using

if (($timestamp = strtotime($str)) === false)

will check for a valid date from a string but how do I determine if the value should even be validated as a date?

Example:

$x = {1,2,3,"4","11/12/2009","22/12/2000",true,false};

foreach($x as $value)
{

if(is_bool($value))

if(is_string($value))

if(is_numeric($value))

if(is_date($value)) ?

...

}

In short, is there an easy way to check if a string value is a date?

Upvotes: 12

Views: 41344

Answers (3)

iateadonut
iateadonut

Reputation: 2239

Extrapolating from https://www.php.net/checkdate#113205 ; just change the $formats array to all the formats you want to check.

public function convertDate($value) {

    $formats = ['M d, Y', 'Y-m-d'];
    foreach($formats as $f) {
        $d = DateTime::createFromFormat($f, $value);
        $is_date = $d && $d->format($f) === $value;

        if ( true == $is_date ) break;
    }

    return $is_date;

}

Upvotes: 1

Pekka
Pekka

Reputation: 449415

In short, is there an easy way to check if a string value is a date?

Not really, seeing as it could be in an arbitrary format.

If at all possible, I would tend to leave parsing to the magic of strtotime(). If it manages to create a valid date, fine. If it doesn't, you'll receive false.

Be prepared for the possibility of false positives, though, because strtotime() parses even things like "Last Friday".

If strtotime() is too liberal for you, you could consider building a collection of date formats you want to accept, and run PHP 5.3's DateTime:createFromFormat using every one of the formats on every date.

Something like (untested)

$formats = array("d.m.Y", "d/m/Y", "Ymd"); // and so on.....
$dates = array(1,2,3,"4","11/12/2009","22/12/2000",true,false);

foreach ($dates as $input) 
 { 
   foreach ($formats as $format)
    {
      echo "Applying format $format on date $input...<br>";

      $date = DateTime::createFromFormat($format, $input);
      if ($date == false) 
       echo "Failed<br>";
      else
       echo "Success<br>";
    }
 }

Upvotes: 20

Dave
Dave

Reputation: 3448

The problem with Pekka's script is that the date '2011-30-30' is also considered valid. This is the modified version.

$formats = array("d.m.Y", "d/m/Y", "Ymd"); // and so on.....
$dates = array(1,2,3,"4","11/12/2009","22/12/2000",true,false);

foreach ($dates as $input) 
 { 
   foreach ($formats as $format)
    {
      echo "Applying format $format on date $input...<br>";

      $date = DateTime::createFromFormat($format, $input);
      if ($date == false || !(date_format($date,$format) == $input) ) 
       echo "Failed<br>";
      else
       echo "Success<br>";
    }
 }

Upvotes: 12

Related Questions