Is date or not in php?

Determine if string is a date, even if string is a non-standard format

(strtotime doesn't accept any custom format)

format($format) === $dateStr);
}

// These return true
validateDateTime('2001-03-10 17:16:18', 'Y-m-d H:i:s');
validateDateTime('2001-03-10', 'Y-m-d');
validateDateTime('2001', 'Y');
validateDateTime('Mon', 'D');
validateDateTime('March 10, 2001, 5:16 pm', 'F j, Y, g:i a');
validateDateTime('March 10, 2001, 5:16 pm', 'F j, Y, g:i a');
validateDateTime('03.10.01', 'm.d.y');
validateDateTime('10, 3, 2001', 'j, n, Y');
validateDateTime('20010310', 'Ymd');
validateDateTime('05-16-18, 10-03-01', 'h-i-s, j-m-y');
validateDateTime('Monday 8th of August 2005 03:12:46 PM', 'l jS \of F Y h:i:s A');
validateDateTime('Wed, 25 Sep 2013 15:28:57', 'D, d M Y H:i:s');
validateDateTime('17:03:18 is the time', 'H:m:s \i\s \t\h\e \t\i\m\e');
validateDateTime('17:16:18', 'H:i:s');

// These return false
validateDateTime('2001-03-10 17:16:18', 'Y-m-D H:i:s');
validateDateTime('2001', 'm');
validateDateTime('Mon', 'D-m-y');
validateDateTime('Mon', 'D-m-y');
validateDateTime('2001-13-04', 'Y-m-d');

With DateTime you can make the shortest date&time validator for all formats.

function validateDate($date, $format = 'Y-m-d H:i:s')
{
   
$d = DateTime::createFromFormat($format, $date);
    return
$d && $d->format($format) == $date;
}
var_dump(validateDate('2012-02-28 12:12:12')); # true
var_dump(validateDate('2012-02-30 12:12:12')); # false
var_dump(validateDate('2012-02-28', 'Y-m-d')); # true
var_dump(validateDate('28/02/2012', 'd/m/Y')); # true
var_dump(validateDate('30/02/2012', 'd/m/Y')); # false
var_dump(validateDate('14:50', 'H:i')); # true
var_dump(validateDate('14:77', 'H:i')); # false
var_dump(validateDate(14, 'H')); # true
var_dump(validateDate('14', 'H')); # truevar_dump(validateDate('2012-02-28T12:12:12+02:00', 'Y-m-d\TH:i:sP')); # true
# or
var_dump(validateDate('2012-02-28T12:12:12+02:00', DateTime::ATOM)); # truevar_dump(validateDate('Tue, 28 Feb 2012 12:12:12 +0200', 'D, d M Y H:i:s O')); # true
# or
var_dump(validateDate('Tue, 28 Feb 2012 12:12:12 +0200', DateTime::RSS)); # true
var_dump(validateDate('Tue, 27 Feb 2012 12:12:12 +0200', DateTime::RSS)); # false
# ...

Before taking any action with date input, it always a great idea to validate the date string. Date validation helps to check whether the provided string is a valid date format. Using the DateTime class you can easily check if the date string is valid in PHP.

In the example code snippet, we will show you how to validate a date string in PHP. It is very useful for server-side validation of the date input using PHP.

The validateDate() function checks whether the given string is a valid date using PHP. It uses PHP DateTime class to validate date based on the specified format. This function returns TRUE if date string is valid, otherwise FALSE.

  • $date – Required. The date string to validate.
  • $format – Optional. The format of the date string.
function validateDate($date$format 'Y-m-d'){
    
$d DateTime::createFromFormat($format$date);
    return 
$d && $d->format($format) === $date;
}

Call the validateDate() function and pass the date string in the first parameter.

// Returns false
var_dump(validateDate('2018-14-01')); 
var_dump(validateDate('20122-14-01'));
var_dump(validateDate('2018-10-32'));
var_dump(validateDate('2017-5-25'));// Returns true
var_dump(validateDate('2018-12-01'));
var_dump(validateDate('1970-11-28'));

By default, the format is set to Y-m-d. If you want to allow day and month without leading zeroes, specify the respective format (Y-n-j).

// Returns true
var_dump(validateDate('2018-2-5''Y-n-j'));

How check value is date or not in PHP?

PHP checkdate() Function var_dump(checkdate(12,31,-400)); echo "
"; var_dump(checkdate(2,29,2003));

Is date a PHP function?

Definition and Usage. The date() function formats a local date and time, and returns the formatted date string.

How do I know the date format?

Date check.
%yyyy%mm%dd..
%yy%mm%dd..
%mm%dd%yyyy..
%mm%dd%yy..
%yyyy%dd%mm..
%yy%dd%mm..
%dd%mm%yyyy..
%dd%mm%yy..

How do you check if a date is between two dates in PHP?

Solution 2.
$stratDate = date('Y-m-d', strtotime("01/01/2018"));.
$endDate = date('Y-m-d', strtotime("01/12/2019"));.
if ((date('Y-m-d', time()) >= $stratDate) && (date('Y-m-d', time()) <= $endDate)){.
echo "true";.
}else{.
echo "false";.