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;
}
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.
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().
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.
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) ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With