Overview

Namespaces

  • None
  • PHP
  • Pry
    • Auth
      • Interfaces
    • Config
    • Controller
    • Date
    • Db
    • Feed
      • Abstracts
      • Writers
    • File
      • Decorator
    • Form
      • Element
    • Image
    • Log
      • Writer
    • Net
      • Exception
    • Session
    • Util
    • Validate
      • Validator
    • View

Classes

  • Alnum
  • Alpha
  • Cp
  • Date
  • Digit
  • Email
  • Equal
  • Interval
  • Time
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Pry Framework
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file LICENSE.txt.
 10:  * 
 11:  */
 12: 
 13: namespace Pry\Validate\Validator;
 14: 
 15: use Pry\Validate\ValidateAbstract;
 16: 
 17: /**
 18:  * Validateur de date.
 19:  * Permet de valider des date du type fr,us,sql,sqldatetime
 20:  * @category Pry
 21:  * @package Validate
 22:  * @subpackage Validate_Validator
 23:  * @version 1.0.0 
 24:  * @author Olivier ROGER <oroger.fr>
 25:  * 
 26:  * <code>
 27:  * $factory = new Validate_Validate();
 28:  * $factory -> addValidator('Date','fr');
 29:  * </code>
 30:  *       
 31:  *
 32:  */
 33: class Date extends ValidateAbstract
 34: {
 35: 
 36:     const DATE_SQL         = 'sql';
 37:     const DATE_SQLDATETIME = 'sqldatetime';
 38:     const DATE_FR          = 'fr';
 39:     const DATE_US          = 'us';
 40: 
 41:     private $format;
 42: 
 43:     public function __construct($format = 'fr')
 44:     {
 45:         $this->errorMsg = "n'est pas une date valide";
 46:         $this->format   = $format;
 47:     }
 48: 
 49:     /**
 50:      * Valide la date en fonction de son format
 51:      *
 52:      * @param string $date
 53:      * @return boolean
 54:      */
 55:     public function isValid($date)
 56:     {
 57:         switch ($this->format)
 58:         {
 59:             case self::DATE_FR:
 60:                 {
 61:                     if (preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2})$`', $date))
 62:                     {
 63:                         list($d, $m, $y) = explode('/', $date);
 64:                         if (checkdate($m, $d, $y))
 65:                             return true;
 66:                     }
 67:                     return false;
 68:                 }
 69:             case self::DATE_US:
 70:                 {
 71:                     if (preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2})$`', $date))
 72:                     {
 73:                         list($m, $d, $y) = explode('/', $date);
 74:                         if (checkdate($m, $d, $y))
 75:                             return true;
 76:                     }
 77:                     return false;
 78:                 }
 79:             case self::DATE_SQL:
 80:                 {
 81:                     if (preg_match('`^(\d{4}|\d{2})-\d{1,2}-\d{1,2}$`', $date))
 82:                     {
 83:                         list($y, $m, $d) = explode('-', $date);
 84:                         if (checkdate($m, $d, $y))
 85:                             return true;
 86:                     }
 87:                     return false;
 88:                 }
 89:             case self::DATE_SQLDATETIME:
 90:                 {
 91:                     if (preg_match('`^(\d{4}|\d{2})-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}$`', $date))
 92:                     {
 93:                         list($date, $heure) = explode(' ', $date);
 94:                         list($y, $m, $d) = explode('-', $date);
 95:                         list($h, $mi, $s) = explode(':', $heure);
 96:                         if (checkdate($m, $d, $y) && ($h >= 0 && $h < 24) && ($mi >= 0 && $mi < 60) && ($s >= 0 && $s < 60))
 97:                             return true;
 98:                     }
 99:                     return false;
100:                 }
101:             default:
102:                 break;
103:         }
104:     }
105: 
106: }
Pry API documentation generated by ApiGen 2.8.0