Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I check if a variable type is DateTime

Tags:

php

datetime

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;
}
like image 266
rzqr Avatar asked Oct 16 '13 06:10

rzqr


People also ask

Is datetime a type in Python?

In Python, date and time are not a data type of their own, but a module named datetime can be imported to work with the date as well as time. Python Datetime module comes built into Python, so there is no need to install it externally. Python Datetime module supplies classes to work with date and time.

How do you define a datetime variable?

To declare a date variable, use the DECLARE keyword, then type the @variable_name and variable type: date, datetime, datetime2, time, smalldatetime, datetimeoffset. In the declarative part, you can set a default value for a variable. The most commonly used default value for a date variable is the function Getdate().

How do you check if a value is a date in Python?

To check if a string is a date, you can use the Python strptime() function from the datetime module. strptime() takes a string and a date format.


1 Answers

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) ...
like image 196
Hokusai Avatar answered Sep 20 '22 22:09

Hokusai