Verify Valid Date Using PHP's Datetime Class

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;
}

php check if valid date and time

function validateDate($date) {
$format = 'Y-m-d h:i A'; // Eg : 2014-09-24 10:19 PM
$dateTime = DateTime::createFromFormat($format, $date);

if ($dateTime instanceof DateTime && $dateTime->format('Y-m-d h:i A') == $date) {
return $dateTime->getTimestamp();
}

return false;
}

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!

validate invalid date character using php

Try This :

E better class for validating and formatting dates Without DateTime function :

http://www.tonymarston.net/php-mysql/dateclass.html

Or

You could use PHP's DateTime class:

function validateDate($date)
{
$d = DateTime::createFromFormat('Y/m/d', $date);
return $d && $d->format('Y/m/d') == $date;
}

function was copied from this answer or php.net

How to check format of Date in PHP?

From the PHP manual on strtotime

Dates in the m/d/y or d-m-y formats are disambiguated by looking at
the separator between the various components: if the separator is a
slash (/), then the American m/d/y is assumed; whereas if the
separator is a dash (-) or a dot (.), then the European d-m-y format
is assumed. To avoid potential ambiguity, it's best to use ISO 8601
(YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

The reason passing the date to SQL server works as mm/dd/yyyy is because of the separator. Where possible it is always best to pass as YYYY-MM-DD as per ISO 8601 which was created for exactly this purpose. To fix your problem the best bet is to change the jQuery plugin configuration to output data in that format (if that's not possible, string replace / with - where it's coming from the jQuery plugin. This will avoid future complications by writing code to fix the date format.

You will no be able to tell the difference between mm/dd/yyyy and dd/mm/yyyy when you don't know where it's come from.

Function to check if a string is a date

If that's your whole string, then just try parsing it:

if (DateTime::createFromFormat('Y-m-d H:i:s', $myString) !== false) {
// it's a date
}

Symfony Validator doesnt check DateTime correctly

I did so, that i just createt a new validator and check the string itself before i add it to my Doctrine Class.

foreach($getData['date'] as $key => $value) {
$validator = Validation::createValidator();
if(!isset($value['startdate'])){
return new View('startdate doesnt exist.');
} elseif(!isset($value['enddate'])){
return new View('enddate doesnt exist.');
}
foreach($value as $value1){
$violations = $validator->validate($value1, array(
new NotBlank(),
new DateTime(),
));
if (count($violations)>0) {
return new View('date isnt valid!');
}
}


Related Topics



Leave a reply



Submit