Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New Coder
    Join Date
    Aug 2010
    Posts
    45
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Date and time validation

    Date validation:

    PHP Code:

        
    class DateValidator {

          private 
    $_date = array();
          private 
    $_lost;

          public function 
    __construct($date null$lost false) {

            
    $this->_lost $lost;

            switch (
    true) {

              case (
    strpos($date'/')):
                
    $date explode('/'$date);
                break;
              case (
    strpos($date'-')):
                
    $date explode('-'$date);
                break;
              case (
    strpos($date'.')):
                
    $date explode('.'$date);
                break;

            }

            if (!isset(
    $date[2][1]) || isset($date[0][2])) {
              
    $this->_date $date;
            }

            else {
              
    $this->_date array_reverse($date);
            }

          }

          public function 
    isValidDay() {

            if (!isset(
    $this->_date[2]) || !ctype_digit($this->_date[2]) || $this->_date[2] < || $this->_date[2] > 31) {
              return 
    false;
            }

            return 
    true;

          }

          public function 
    isValidMonth() {

            if (!isset(
    $this->_date[1]) || !ctype_digit($this->_date[1]) || $this->_date[1] < || $this->_date[1] > 12) {
              return 
    false;
            }

            return 
    true;

          }

          public function 
    isValidYear() {

            if (!isset(
    $this->_date[0]) || !ctype_digit($this->_date[0]) || $this->_date[0] == 0) {
              return 
    false;
            }

            if (
    $this->_date[0] == 0) {
              return 
    false;
            }

            return 
    true;

          }

          public function 
    isValidDate() {

            if (!
    $this->isValidDay() || !$this->isValidMonth() || !$this->isValidYear()) {
              return 
    false;
            }

            if (isset(
    $this->_date[3])) {
              return 
    false;
            }

            if (
    $this->_date[2] == 31 && in_array($this->_date[1], array(46911))) {
              return 
    false;
            }

            if (
    $this->_date[2] == 30 && $this->_date[1] == 2) {
              return 
    false;
            }

            if (
    $this->_date[2] == 29 && $this->_date[1] == 2) {

              if (!
    is_int($this->_date[0] / 4)) {
                return 
    false;
              }

              if (!
    is_int($this->_date[0] / 400) && is_int($this->_date[0] / 100)) {
                return 
    false;
              }

            }

            if (
    $this->_lost && $this->_date[0] == 1752 && $this->_date[1] == && $this->_date[2] > && $this->_date[2] < 14) {
              return 
    false;
            }

            return 
    true;

          }

          public function 
    returnDate() {

            if (!
    $this->isValidDate()) {
              return 
    '';
            }

            if (!isset(
    $this->_date[2][1])) {
              
    $this->_date[2] = '0' $this->_date[2];
            }

            if (!isset(
    $this->_date[1][1])) {
              
    $this->_date[1] = '0' $this->_date[1];
            }

            return 
    implode('-'$this->_date);

          }

        } 
    Dates can be input with either the day or the year first (the month must be second); separated with a forward-slash, a dash-minus, a dot, or a space (mixing is not allowed), and a single leading zero is allowed if the day or month is between 1 and 9 (inclusive). The year must be four digits long and not equal to 0000.

    Example:

    PHP Code:

    $validDate 
    = new DateValidator('2000/02/30');

    var_dump($validDate->isValidDay()); // bool(true)
    var_dump($validDate->isValidMonth()); // bool(true)
    var_dump($validDate->isValidYear()); // bool(true)
    var_dump($validDate->isValidDate()); // bool(false) (29 or 28 days in February)

    $validDate = new DateValidator('1 1 2001');

    echo 
    $validDate->returnDate(); // 2001-01-01 
    If you pass a true value as the optional second parameter in the constructor method then any date between 3 September 1752 and 13 September 1752 will return false. I added this just for fun; when the Julian calendar was switched to the Gregorian calendar in Britain, 11 days needed to be removed to retain accuracy. As such, the 13th followed the 3rd. The dates in between never actually happened.

    Time validation:

    PHP Code:

        
    class TimeValidator {

          private 
    $_time = array();

          public function 
    __construct($time null) {

            switch (
    true) {

              case (
    strpos($time':')):
                
    $this->_time explode(':'$time);
                break;
              case (
    strpos($time'-')):
                
    $this->_time explode('-'$time);
                break;
              case (
    strpos($time'.')):
                
    $this->_time explode('.'$time);
                break;
              case (
    strpos($time' ')):
                
    $this->_time explode(' '$time);
                break;

            }

          }

          public function 
    isValidSecond() {

            if (!isset(
    $this->_time[2]) || !ctype_digit($this->_time[2]) || $this->_time[2] > 59) {
              return 
    false;
            }

            return 
    true;

          }

          public function 
    isValidMinute() {

            if (!isset(
    $this->_time[1]) || !ctype_digit($this->_time[1]) || $this->_time[1] > 59) {
              return 
    false;
            }

            return 
    true;

          }

          public function 
    isValidHour() {

            if (!isset(
    $this->_time[0]) || !ctype_digit($this->_time[0]) || $this->_time[0] > 24) {
              return 
    false;
            }

            return 
    true;

          }

          public function 
    isValidTime() {

            if (!
    $this->isValidSecond() || !$this->isValidMinute() || !$this->isValidHour()) {
              return 
    false;
            }

            if (
    $this->_time[0] == 24 && ($this->_time[1] != || $this->_time[2] != 0)) {
              return 
    false;
            }

            return 
    true;

          }

          public function 
    returnTime() {

            if (!
    $this->isValidTime()) {
              return 
    '';
            }

            if (!isset(
    $this->_time[0][1])) {
              
    $this->_time[0] = '0' $this->_time[0];
            }

            if (!isset(
    $this->_time[1][1])) {
              
    $this->_time[1] = '0' $this->_time[1];
            }

            if (!isset(
    $this->_time[2][1])) {
              
    $this->_time[2] = '0' $this->_time[2];
            }

            return 
    implode(':'$this->_time);

          }

        } 
    Time must be input with the hour first, the minute second, and the seconds third, separated with a colon, a dash-minus, a dot, or a space (mixing is not allowed), and a single leading zero is allowed if the units are between 0 and 9 (inclusive). An hour of 24 is allowed only if the minutes and seconds are both (0)0.

    Example:

    PHP Code:

    $validTime 
    = new TimeValidator('24:1:4');

    var_dump($validTime->isValidHour()); // bool(true)
    var_dump($validTime->isValidMinute()); // bool(true)
    var_dump($validTime->isValidSecond()); // bool(true)
    var_dump($validTime->isValidTime()); // bool(false) (hour of 24 allowed only if minutes and seconds are (0)0)

    $validTime = new TimeValidator('1 1 1');

    echo 
    $validTime->returnTime(); // 01:01:01 
    Last edited by MRushton; 10-22-2010 at 08:33 PM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •