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
How to See the Actual Xml Generated by PHP Soap Client Class
A _Construct on an Eloquent Laravel Model
How to Serve an Image with Laravel
Multi-Dimensional Array Post from Form
How to Measure the Strength of a Password
How to Get All Checkbox Variables Even If Not Checked from HTML to PHP
How to Run Matlab Code for Isolated Spoken Words Recognition from PHP
Copy Large Files (Over 2 Gb) in PHP
How to Get PHP, Symlinks and _File_ to Work Together Nicely
Troubleshooting "The Use Statement with Non-Compound Name ... Has No Effect"
How to Check If a Longitude/Latitude Point Is Within a Range of Coordinates
How to Decode a JSON String with Several Objects in PHP
Checking If a String Contains an Integer
Wordpress Woocommerce - Use Update_Post_Meta to Add Product Attributes