PHP Classes

How Can PHP Validate Date According to Valid Date Formats using Date Validator: Validate strings with dates to match a format

Recommend this page to a friend!
  Info   View files Example   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 93 This week: 1All time: 9,876 This week: 560Up
Version License PHP version Categories
date-validator 1.0MIT/X Consortium ...7.4Time and Date, Validation, PHP 7
Description 

Author

This package can validate strings with dates to match a format.

It can string with a date, a time, or a date and a time and validates that string according to a format.

The package can try validating the date or time string to check if it matches one of the several formats defined within the validation class.

You can add more date and time formats to let make the package support more valid formats.

Innovation Award
PHP Programming Innovation award nominee
November 2022
Number 2
Many applications take input parameters from users or other external data sources, including dates or times.

When the applications allow multiple formats for the date and time values they accept as parameters, they must check them against all the acceptable date and time formats.

This package can perform date and time validation that accepts many possible formats and can even support more custom date and time formats.

Manuel Lemos
Picture of Francisco Núñez
  Performance   Level  
Name: Francisco Núñez <contact>
Classes: 6 packages by
Country: Panama Panama
Age: 37
All time rank: 36813 in Panama Panama
Week rank: 411 Up1 in Panama Panama Up
Innovation award
Innovation award
Nominee: 2x

Example

<?php

declare(strict_types=1);

use
IcarosNet\DateValidator\DateValidator;

require_once
__DIR__."/../vendor/autoload.php";

$date_validator = new DateValidator();

//String parsing and main date formats:

var_dump($date_validator->ValidateDate('10/10/1999 20:40'));
echo
'<br>';
var_dump($date_validator->ValidateDate('09/09/2010 08:40 PM'));
echo
'<br>';

//Analysis (Validation) and Date Format Conversion:
echo 'Correct date->10/10/1999 20:40 a: ';
if(
$date_validator->ValidateDate('10/10/1999 20:40')){
   
var_dump($date_validator->FormatDate('10/10/1999 20:40'));
}

echo
'<br>';
echo
'Correct date->09/09/2010 08:40 PM to: ';
if(
$date_validator->ValidateDate('09/09/2010 08:40 PM')){
   
var_dump($date_validator->FormatDate('09/09/2010 08:40 PM'));
}

echo
'<br>';
echo
'Incorrect Date->09/XX/2010 0N:40 PM to: ';
if(
$date_validator->ValidateDate('09/XX/2010 0N:40 PM')){
   
var_dump($date_validator->FormatDate('09/XX/2010 0N:40 PM'));
}else{
    echo
'Format not supported';
}

echo
'<br>';
echo
'Bad Date->09--09--2010 20%40 to: ';
if(
$date_validator->ValidateDate('09--09--2010 20%40')){
   
var_dump($date_validator->FormatDate('09--09--2010 20%40'));
}else{
    echo
'Format not supported';
}

//Inserting a different format to the analysis to support it in the validation:
echo '<br>';
echo
'Date new format d--m--Y H%i -> : ';
var_dump($date_validator->addFormat('d--m--Y H%i')->ValidateDate('09--09--2010 20%40'));


Details

Date-Validator

Validation of format is right for given datetime,date or time string variable.

Acronym: [Date-Validator].

Name: Date-Validator.

Dependencies: Stand Alone / PHP v7.4.

What does [Date-Validator] do?

is a very simple PHP [Date-Validator] implementation that allows you to easily validate if the PHP variable passed is a string with valid datetime, date, time format.

Why use [Date-Validator]?

Developers need the ability to validate if variable meaning a datetime, date or time have a right format, this helps to validate some format dependencies that must have a datetime, date or time string.

Help to improve [Date-Validator]?

if you want to collaborate with the development of the library; You can express your ideas or report any situation related to this in: https://github.com/arcanisgk/Date-Validator/issues

[Date-Validator] Configuration:

None necessary.

[Date-Validator] Installation:

composer require arcanisgk/date-validator

[Date-Validator] Usage:

Instance of Class:


use IcarosNet\DateValidator\DateValidator;
require __DIR__.'\..\vendor\autoload.php';
$date_validator = new DateValidator();

Implementation of Class:


if ($date_validator->ValidateDate('10/10/1999 20:40')) {
    echo 'Correct date string';
}

if ($date_validator->ValidateDate('XXX10/10/1999 20:40')) {
     echo 'incorrect date string';
}

if($date_validator->addFormat('d--m--Y H%i')->ValidateDate('09--09--2010 20%40')){
     echo 'Correct new date format string using % separator';
}

Contributors

  • (c) 2020 - 2022 Walter Francisco Núñez Cruz icarosnet@gmail.com Donate

  Files folder image Files  
File Role Description
Files folder imageexample (1 file)
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  example  
File Role Description
  Accessible without login Plain text file index.php Example Example script

  Files folder image Files  /  src  
File Role Description
  Plain text file DateValidator.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:93
This week:1
All time:9,876
This week:560Up