16

How can I check if a variable is a datetime type on php, please? I've tried this, but it seems doesn't work.

public function setRegistrationDate ($registrationDate) {

   if (!is_date($registrationDate, "dd/mm/yyyy hh:mm:ss")) {
       trigger_error('registrationDate != DateTime', E_USER_WARNING);
       return;
   }

   $this->_registrationDate = $registrationDate;
}
rzqr
  • 259
  • 2
  • 3
  • 10

4 Answers4

58

I think this way is more simple:

if (is_a($myVar, 'DateTime')) ...

This expression is true if $myVar is a PHP DateTime object.

Since PHP 5, this can be further simplified to:

if ($myVar instanceof DateTime) ...
vog
  • 23,517
  • 11
  • 59
  • 75
Hokusai
  • 2,219
  • 1
  • 21
  • 22
  • 4
    This should be the correct answer. Since the other answers are old their approaches are outdated. This is the correct way to verify class type in PHP. As a sidenote, single qutoes should be used as double quotes causes an extra step for variable interpolation that is not needed in these circumstances. – Ryan Rentfro Mar 06 '16 at 21:04
  • You are right. I just changed expression from double quotes to single quotes. Thanks Ryan. – Hokusai Mar 07 '16 at 10:59
  • I think the original question was how to determine whether a given string matches a particular format, rather than whether a particular PHP variable is an instance of a particular class. As for single versus double quotes, the difference is negligible and worrying about such things is a micro-optimisation. If your code is slow it's not slow because you used double-quotes. https://nikic.github.io/2012/01/09/Disproving-the-Single-Quotes-Performance-Myth.html – GordonM Mar 07 '16 at 11:00
  • The code in the question from OP is in fact trying to verify type on line 3. As for micro optimization that isn't really the point. It's about using the correct string enclosure. It is negligible but its a matter of convention. Not a matter of slowness but a matter of correct practice. See https://en.wikipedia.org/wiki/Coding_conventions – Ryan Rentfro Mar 07 '16 at 16:25
  • +1 Ryan. It makes sense to use the right enclosure, the only reason not to is laziness (though to be fair the PHP docs themselves generally use the wrong enclosures). Also, though it is a "micro" optimization, it's one that it multiplied countless times in a large codebase. It won't make or break anything but there is no logic in not doing it. – Ian Apr 09 '16 at 19:06
  • the best simple answer I have seen so far – Budianto IP May 08 '19 at 06:49
9
function validateDate($date, $format = 'Y-m-d H:i:s')
{
    $d = DateTime::createFromFormat($format, $date);
    return $d && $d->format($format) == $date;
}

Reference:

Community
  • 1
  • 1
Daniele Vrut
  • 2,835
  • 2
  • 22
  • 32
  • 1 minutes to validate this, is it a good practice to regroup all of the business classes in aonly one file when we're OO php programming, please ? – rzqr Oct 16 '13 at 06:51
6

The simplest answer is : to check with strtotime() function

$date = strtotime($datevariable);

If it's valid date, it will return timestamp, otherwise returns FALSE.

Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
  • Thanks for response, one more question please, is it a good practice to regroup all of the business classes in aonly one file when we're OO php programming, please ? – rzqr Oct 16 '13 at 06:56
  • 1
    we should put one class in one file ideally. – Nishu Tayal Oct 16 '13 at 10:49
  • 1
    No, I do not get false I get an error: Warning: strtotime() expects parameter 1 to be string, object give – peace_love Apr 16 '20 at 12:16
2

I would use the following method:

public function setRegistrationDate ($registrationDate) {
  if(!($registrationDate instanceof DateTime)) {
    $registrationDate = date_create_from_format("dd/mm/yyyy hh:mm:ss", $registrationDate)
  }

  if(!($registrationDate instanceof DateTime)) {
    trigger_error('registrationDate is not a valid date', E_USER_WARNING);
    return false;
  }

  $this->_registrationDate = $registrationDate;
  return true
}
Matteo Tassinari
  • 18,121
  • 8
  • 60
  • 81
  • Thanks for response, one more question please, is it a good practice to regroup all of the business classes in aonly one file when we're OO php programming, please ? – rzqr Oct 16 '13 at 06:55
  • 1
    I usually tend to put one class in one file, following this convention (https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md) for better autoloading. – Matteo Tassinari Oct 16 '13 at 07:01