A Class for Validating and Formatting Dates

by Tony Marston
TonyMarston.net


Display date to the User

This function will take a date from the database (although it could come from other sources) and format it ready for display to the user.

function getExternalDate ($input)

   {

This first part checks for input in the format yyyymmdd.

     if (strlen($input) == 8) {

         $pattern = '(^[0-9]{4})'    // 4 digits (yyyy)

                   .'([0-9]{2})'     // 2 digits (mm)

                   .'([0-9]{2}$)';   // 2 digits (dd)

         if (ereg($pattern, $input, $regs)) {

            if (!checkdate($regs[2], $regs[3], $regs[1])) {

               $this->errors = 'This is not a valid date';

               return FALSE;

            } else {

               $monthnum = (int)$regs[2];

               $this->externaldate = "$regs[3] " .$this->monthalpha[$monthnum] ." $regs[1]";

               return $this->externaldate;

            } // if

         } // if

         $this->errors = "Invalid date format: expected 'yyyymmdd'";

         return FALSE;

      } // if

If this does not find a match this next part checks for input in the format yyyy-mm-dd.

 if (strlen($input) == 10) {

         $pattern = '(^[0-9]{4})'   // 4 digits (yyyy)

                   .'([^0-9])'      // not a digit

                   .'([0-9]{2})'    // 2 digits (mm)

                   .'([^0-9])'      // not a digit

                   .'([0-9]{2}$)';  // 2 digits (dd)

         if (ereg($pattern, $input, $regs)) {

            if (!checkdate($regs[3], $regs[5], $regs[1])) {

               $this->errors = 'This is not a valid date';

               return FALSE;

            } else {

               $monthnum = (int)$regs[3];

               $this->externaldate = "$regs[5] "  .$this->monthalpha[$monthnum] ." $regs[1]";

               return $this->externaldate;

            } // if

         } // if

         $this->errors = "Invalid date format: expected 'dd-mm-yyyy'";

         return FALSE;

      } // if


If we have not found a match against any of these patterns then we generate an error message before returning an empty result to the user.

$this->errors = 'This is not a valid date';

      return $input;

     

   } // getExternalDate



Options:
Printer Friendly
Email Friend

About The Author:

I have been a software engineer, both designing and developing, since 1977. I have worked with a variety of 2nd, 3rd and 4th generation languages on a mixture of mainframes, mini- and micro-computers. I have worked with flat files, indexed files, hierarchical databases, network databases and relational databases. The user interfaces have included punched card, paper tape, teletype, block mode, CHUI, GUI and web. I have written code which has been procedural, model-driven, event-driven, component-based and object oriented. I have built software using the 1-tier, 2-tier, 3-tier and Model-View-Controller (MVC) architectures. After working with COBOL for 16 years I switched to UNIFACE in 1993, starting with version 5, then progressing through version 6 to version 7. In the middle of 2002 I decided to teach myself to develop web applications using PHP and MySQL.

Developer Categories



Developer Tutorials
ASP
CGI & Perl
CSS
Flash
HTML
Java
JavaScript
MySQL
PHP
Python
XML

Web Hosting

BlueHost Hosting - Unlimited Bandwidth, Unlimited Disk Space, Unlimited Domains for only $3.95!

Developer Documentation

Developer Tools



Search our Developer Tutorials
  The DevSyndicate Network