php date validation
You could use checkdate. For example, something like this:
$test_date = '03/22/2010';
$test_arr = explode('/', $test_date);
if (checkdate($test_arr[0], $test_arr[1], $test_arr[2])) {
// valid date ...
}
A more paranoid approach, that doesn't blindly believe the input:
$test_date = '03/22/2010';
$test_arr = explode('/', $test_date);
if (count($test_arr) == 3) {
if (checkdate($test_arr[0], $test_arr[1], $test_arr[2])) {
// valid date ...
} else {
// problem with dates ...
}
} else {
// problem with input ...
}
Correctly determine if date string is a valid date in that format
You can use DateTime::createFromFormat()
for this purpose:
function validateDate($date, $format = 'Y-m-d')
{
$d = DateTime::createFromFormat($format, $date);
// The Y ( 4 digits year ) returns TRUE for any integer with any number of digits so changing the comparison from == to === fixes the issue.
return $d && $d->format($format) === $date;
}
[Function taken from this answer. Also on php.net. Originally written by Glavić.]
Test cases:
var_dump(validateDate('2013-13-01')); // false
var_dump(validateDate('20132-13-01')); // false
var_dump(validateDate('2013-11-32')); // false
var_dump(validateDate('2012-2-25')); // false
var_dump(validateDate('2013-12-01')); // true
var_dump(validateDate('1970-12-01')); // true
var_dump(validateDate('2012-02-29')); // true
var_dump(validateDate('2012', 'Y')); // true
var_dump(validateDate('12012', 'Y')); // false
Demo!
PHP check user input date
You can use a combination of strtotime()
and checkdate()
to see if the date is valid:
function isRealDate($date) {
if (false === strtotime($date)) {
return false;
}
list($year, $month, $day) = explode('-', $date);
return checkdate($month, $day, $year);
}
usage
if (isRealDate($geboortedatum)) {
// date is ok
}
else {
// date is not ok
}
PHP Regex to check date is in YYYY-MM-DD format
Try this.
$date="2012-09-12";
if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date)) {
return true;
} else {
return false;
}
Date validation from form input with PHP
Try this. I recommend you to use DateTime()
object instead of time()
function.
$checkinDate = new DateTime('2021-05-14 09:52:52'); //use your actual POST value here
if ($checkinDate){
$today = new DateTime();
$diff = $checkinDate->diff($today);
$days= $diff->format('%a');
if($days>14){
echo "2 Weeks done. Please enter a new date";
}
else{
echo "Not done 2 weeks yet.";
}
}
Verify valid date using PHP's DateTime class
You can try this one:
static public function verifyDate($date)
{
return (DateTime::createFromFormat('m/d/Y', $date) !== false);
}
This outputs true/false. You could return DateTime
object directly:
static public function verifyDate($date)
{
return DateTime::createFromFormat('m/d/Y', $date);
}
Then you get back a DateTime
object or false on failure.
UPDATE:
Thanks to Elvis Ciotti who showed that createFromFormat accepts invalid dates like 45/45/2014.
More information on that: https://stackoverflow.com/a/10120725/1948627
I've extended the method with a strict check option:
static public function verifyDate($date, $strict = true)
{
$dateTime = DateTime::createFromFormat('m/d/Y', $date);
if ($strict) {
$errors = DateTime::getLastErrors();
if (!empty($errors['warning_count'])) {
return false;
}
}
return $dateTime !== false;
}
Validate date format in php
function _date_is_valid($str) {
if (substr_count($str, '/') == 2) {
list($d, $m, $y) = explode('/', $str);
return checkdate($m, $d, sprintf('%04u', $y));
}
return false;
}
Related Topics
Fatal Error: Call to a Member Function Prepare() on Null
How to Validate Google Recaptcha V3 on Server Side
Why Does the PHP Json_Encode Function Convert Utf-8 Strings to Hexadecimal Entities
How to Re-Index the Values of an Array in PHP
Laravel 5 Failed Opening Required Bootstrap/../Vendor/Autoload.PHP
PHP-Intl Installation on Xampp
Interpolation (Double Quoted String) of Associative Arrays in PHP
Can You Store a Function in a PHP Array
PHP - Merging Two Arrays into One Array (Also Remove Duplicates)
When to Use Strtr VS Str_Replace
Get in Woocommerce Cart the Product Id of a Cart Item
What's the Difference Between ++$I and $I++ in PHP
Yii2 Htaccess - How to Hide Frontend/Web and Backend/Web Completely
Curl_Exec() Always Returns False